Reputation: 6612
I have an external parsed List type which is a json array, indeed this call
list.toString()
gives me this:
[{name=prop1, content=<something>},{name=prop2, content=<something>}]
The content could be anything, an array, a string, an int... So I'd like to map that json array to something like
HashMap<String,Object> converted;
This would allow me to do this:
converted.get("prop1"); //this will give me the <something>
how could I do that?
Upvotes: 2
Views: 6194
Reputation: 3456
Try this using JSON Parser. Get the JSON lib from here json.simple
Map<String, Object> map = new HashMap<>();
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(list);
JSONArray jsonArray = (JSONArray)obj;
Iterator<JSONObject> iterator = jsonArray.iterator();
while (iterator.hasNext()) {
JSONObject jsonObj = iterator.next();
String name = (String) jsonObject.get("name");
Object content = (Object) jsonObject.get("content");
map.put(name, content);
}
} catch (ParseException p) { }
Upvotes: 2