迭代 json 对象列表?



我必须遍历 json 对象列表("local_properties"(,并检查每个对象是否"is_built_in"= false。我应该怎么做。我正在获得以下 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
}
]
}

考虑使用 Jackson 对响应进行解散,然后您将能够遍历"local_properties"并在"is_built_in"为真时执行给定的操作。以下代码片段显示了实现此目的的一种方法,假设您将能够将 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": truen" +
"    },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": truen" +
"    }]}";
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
}
});

相关内容

  • 没有找到相关文章

最新更新