简体   繁体   English

Java - 如何遍历 hashmap 的列表?

[英]Java - How to iterate over a list of hashmap?

I have a following response from a HTTP call which looks like this...我收到来自 HTTP 调用的以下响应,看起来像这样......

[{"id": 1, "name" : abc, "above50" :  true} , {"id": 2, "name" : "xyc", "above50" :  false, "kids" : "yes"} ]

I need to iterate through this list and find if there is a key called kids and if there is the key kids, i need to store the value.我需要遍历此列表并查找是否有一个名为 kids 的密钥,如果有密钥 kids,我需要存储该值。 How do i do it in java?我如何在 java 中做到这一点?

First you need to parse the json string - it's a list of objects.首先,您需要解析 json 字符串 - 它是一个对象列表。 If you don't have classes to match those objects, by default they can be represented as Map<String, Object> .如果您没有匹配这些对象的类,默认情况下它们可以表示为Map<String, Object> Then you need to iterate the list, and for every object in it, you have to iterate the entries in the object.然后你需要迭代列表,对于其中的每个 object,你必须迭代 object 中的条目。 If the key matches, store it.如果密钥匹配,则存储它。

        //parse json string with whatever parser you like
        List<Map<String, Object>> list = ...;
        //iterate every object in the list
        for (Map<String, Object> map : list) {
            //iterate every entry in the object
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (entry.getKey().equals("kids")) {
                    //you can store the key and the value however you want/need
                    System.out.println(entry.getKey() + " -> " + entry.getValue());
                }
            }
        }
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-------------------------------------------

    @Test
    public void test04() throws IOException {
        final String preString = "[{\"id\": 1, \"name\" : \"abc\", \"above50\" :  true} , {\"id\": 2, \"name\" : \"xyc\", \"above50\" :  false, \"kids\" : \"yes\"} ]";
        final ObjectMapper objectMapper = new ObjectMapper();
        final JsonNode arrayNode = objectMapper.readTree(preString);
        if (arrayNode.isArray()) {
            for (JsonNode it : arrayNode) {
                final JsonNode kids = it.get("kids");
                if (kids != null) {
                    //TODO: Storage this value by you want
                    System.out.println(kids.asText());
                }
            }
        }
    }
 

You can use JSONObject or JSONArray您可以使用 JSONObject 或 JSONArray

String message = ""list" : [{"id": 1, "name" : abc, "above50" :  true} , {"id": 2, "name" : "xyc", "above50" :  false, "kids" : "yes"} ]";
JSONObject jsonObject = new JSONObject(message);
JSONArray array = jsonObject.getJsonArray("list");
//so now inside the jsonArray there is 2 jsonObject
//then you can parse the jsonArray and check if there is 
//a jsonObject that have "kids" like jsonObject.get("kids") != null
// or jsonObject.getString("kids") != null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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