0

I have a List<String> data which is like:

{"0":["passFrom","3/9/2018","3/9/2018","anotherMethod","but"],"1":["googleForAlongTIme","3/9/2018","3/9/2018","stillCannotConvert","theLinkHashMap"]}

I need to store to a linkeHashMap with the above data, so far I had try something like below.

ArrayList<String> listdata = new ArrayList<String>();
Map<Integer, List<String>> listMap = new LinkedHashMap<Integer, List<String>>();

if (jsonArray.getString(0).trim()!= null && !jsonArray.getString(0).isEmpty()) {
    for (int i = 0; i < jsonArray.length(); i++){ 
        listdata.add(jsonArray.getString(i)); // here is the data which shown above
        //trying to use split at here but find out `**["passFrom","3/9/2018","3/9/2018","anotherMethod","but"],"1"**` is not the correct data

        /*List<String> bothList= Arrays.asList(listdata.get(i).toString().split(":"));
        for (String string : bothList) {
             List<String> tempData=Arrays.asList(bothList.toString());
             listMap.put(i, tempData);
             System.out.println("TeST: " + string);
         }*/
    }
}

Need some hints and help here, as my final aim is to get the 0,1 integer and below data to store inside the listMap

"passFrom","3/9/2018","3/9/2018","anotherMethod","but" "googleForAlongTIme","3/9/2018","3/9/2018","stillCannotConvert","theLinkHashMap"

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
薛源少
  • 306
  • 1
  • 6
  • 18
  • So, you have some `String`s which look like JSON ... maybe use a JSON parser to parse them, then you can gain (easier) access to the properties. Maybe even parse them to a POJO, which would make the whole thing simpler to process – MadProgrammer Jul 16 '18 at 23:34
  • yeap, I try to use json parser but the server side do not allowed to added the json identifier. I had no choice to come out something like this at the client side. lol, thanks. – 薛源少 Jul 16 '18 at 23:51

1 Answers1

1

Try this:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ParseJson {

    public static void main(String[] args) throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();

        final String jsonStr = "{\"0\":[\"passFrom\",\"3/9/2018\",\"3/9/2018\",\"anotherMethod\",\"but\"],\"1\":[\"googleForAlongTIme\",\"3/9/2018\",\"3/9/2018\",\"stillCannotConvert\",\"theLinkHashMap\"]}";

        Map<Integer, List<String>> map = objectMapper.readValue(jsonStr, new TypeReference<LinkedHashMap<Integer, List<String>>>(){});

        for (Map.Entry<Integer, List<String>> entry : map.entrySet()) {
            System.out.printf("For item \"%d\", values are:\n", entry.getKey());
            for (String value : entry.getValue()) {
                System.out.printf("\t[%s]\n", value);
            }
        }
    }
}

Outputs:

For item "0", values are:
    [passFrom]
    [3/9/2018]
    [3/9/2018]
    [anotherMethod]
    [but]
For item "1", values are:
    [googleForAlongTIme]
    [3/9/2018]
    [3/9/2018]
    [stillCannotConvert]
    [theLinkHashMap]
Eric Green
  • 1,151
  • 9
  • 17