简体   繁体   中英

Read hashmap with inner array of objects in java

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"));

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM