Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA中如何获取JsonNode中特定字段的值_Java_Json - Fatal编程技术网

JAVA中如何获取JsonNode中特定字段的值

JAVA中如何获取JsonNode中特定字段的值,java,json,Java,Json,我有一些JSON文档。下面是JSON结构 { "header": { "file1":0, "file2":1, "subfiles":{ "subfile1":"true", "subfile2":"true", }

我有一些JSON文档。下面是JSON结构

{
    "header":
    {
        "file1":0,
        "file2":1,
        "subfiles":{
          "subfile1":"true",
          "subfile2":"true",
        }
    },

    "response":
    {
        "number":678,
        "start":0,
        "docs":[
            {
                "id":"d3f3d",
                "code":"l876s",
                "country_name":"United States",
                "city":"LA"
            },
            {
                "id":"d2f2d",
                "code":"2343g",
                "country_name":"UK",
                "city":"London"
            }
        ]
    }
}
我想使用JsonNode获取“id”字段中的值。如何访问这样的结构中的特定字段(id、城市或国家/地区名称)?我尝试使用:

JsonNode node = documentHandle.get();
String s = node.path("id").asText();

但我没有得到除null之外的任何结果。

Jayway的JsonPath在这方面效果很好:

JsonPath.read(jsonString, "$.response.docs[*].id");

Jayway的JsonPath在这方面效果很好:

JsonPath.read(jsonString, "$.response.docs[*].id");

您的节点指向JSON文档的根。要到达
id
,必须遍历路径

ArrayNode docs = node.path("response").path("docs");
for (JsonNode doc: docs) { // this is pseudo-code
  String s = doc.path("id").asText();
}

或者使用
JsonPath

您的节点指向JSON文档的根。要到达
id
,必须遍历路径

ArrayNode docs = node.path("response").path("docs");
for (JsonNode doc: docs) { // this is pseudo-code
  String s = doc.path("id").asText();
}
或者使用
JsonPath