Home > Net >  Read hashmap with inner array of objects in java
Read hashmap with inner array of objects in java

Time:12-24

This is a result I get from a hashmap in java. How do we read this and extract value1 and value2 and put them inside another hash map? And only one array of object inside.

{
    "result": [
        {
            "desc": {
                "value1": "",
                "value2 ""
            }
        }
    ]
}

This is the method tried,

HashMap result = (HashMap) response.get("result");
HashMap desc = (HashMap) result.get("desc");
map.put("value1", desc.get("value1"));
map.put("value2", desc.get("value2"));

CodePudding user response:

You're missing the List level

List<Object> result = (List<Object>) response.get("result");
HashMap<String,Object> item = (HashMap<String,Object>) result.get(0);
HashMap<String,String> desc = (<String,String>) item.get("desc");
map.put("value1", desc.get("value1"));
map.put("value2", desc.get("value2"));
  • Related