简体   繁体   English

迭代json对象列表?

[英]Iterating a list of json object?

I have to iterate over a list of json object ("local_properties") and check in each object if "is_built_in"= false or not.我必须遍历 json 对象列表(“local_properties”)并检查每个对象是否“is_built_in”= false。 How I am supposed to do that.我该怎么做。 I am getting the below json as a response of GET API.我收到以下 json 作为 GET API 的响应。

{
  "id": 1,
  "local_properties": [
    {
      "id": 1000221,
      "name": "RSCD_DIR",
      "data_type": "String",
      "default_value_source": "SystemObject/local",
      "value": "/C/PROGRA~1//",
      "is_built_in": true,
      "is_deprecated": true
    },
    {
      "id": 1000221,
      "name": "RSCD_DIR",
      "data_type": "String",
      "default_value_source": "SystemObject/local",
      "value": "/C/PROGRA~1//",
      "is_built_in": false,
      "is_deprecated": true
    },
    {
      "id": 1000221,
      "name": "RSCD_DIR",
      "data_type": "String",
      "default_value_source": "SystemObject/local",
      "value": "/C/PROGRA/",
      "is_built_in": true,
      "is_deprecated": true
    }
  ]
}

Consider desiarlizing your response using Jackson and then you will be able to iterate through 'local_properties' and perform a given action when 'is_built_in' is true.考虑使用 Jackson 对您的响应进行反序列化,然后您将能够遍历 'local_properties' 并在 'is_built_in' 为真时执行给定的操作。 The following snippet shows one way of achieving this, assuming you will be able to deserialise your json into Jacksons JsonNode.以下代码段展示了实现此目的的一种方法,假设您能够将 json 反序列化为 Jacksons JsonNode。

String sampleJson = "{\n" +
                "  \"id\": 1,\n" +
                "  \"local_properties\": [\n" +
                "    {\n" +
                "      \"id\": 1000221,\n" +
                "      \"name\": \"RSCD_DIR\",\n" +
                "      \"data_type\": \"String\",\n" +
                "      \"default_value_source\": \"SystemObject/local\",\n" +
                "      \"value\": \"/C/PROGRA~1/BMCSOF~1/BLADEL~1/rscd/\",\n" +
                "      \"is_built_in\": true,\n" +
                "      \"is_deprecated\": true\n" +
                "    },\n" +
                "    {\n" +
                "      \"id\": 1000221,\n" +
                "      \"name\": \"RSCD_DIR\",\n" +
                "      \"data_type\": \"String\",\n" +
                "      \"default_value_source\": \"SystemObject/local\",\n" +
                "      \"value\": \"/C/PROGRA~1/BMCSOF~1/BLADEL~1/rscd/\",\n" +
                "      \"is_built_in\": false,\n" +
                "      \"is_deprecated\": true\n" +
                "    }]}";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode json = mapper.readValue(sampleJson, JsonNode.class);
        json.get("local_properties").elements().forEachRemaining(node -> {
            if (node.get("is_built_in").asBoolean()) {
                //Do Something when is_built_in is true
            }
        });

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

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