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
19k10 gold badges97 silver badges113 bronze badges
asked Jul 16, 2018 at 23:32
2
  • So, you have some Strings 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 Commented Jul 16, 2018 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. Commented Jul 16, 2018 at 23:51

1 Answer 1

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]
answered Jul 16, 2018 at 23:44
2
  • This, but in a loop for each element of the List. Commented Jul 16, 2018 at 23:51
  • Fantastic! I'm glad you're pleased. Good luck! Commented Jul 17, 2018 at 4:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.