gpt4 book ai didi

java - 我有一个 json 字符串,但无法提取特定字段

转载 作者:行者123 更新时间:2023-11-30 02:49:37 24 4
gpt4 key购买 nike

我有一个如下所示的 json 字符串:

{
"results" : [
{
"address_components" : [
{
"long_name" : "Arad",
"short_name" : "Arad",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Arad",
"short_name" : "Arad",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Arad County",
"short_name" : "AR",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Romania",
"short_name" : "RO",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Arad, Romania",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 46.2217118,
"lng" : 21.3823449
},
"southwest" : {
"lat" : 46.1286902,
"lng" : 21.230371
}
},
"location" : {
"lat" : 46.1865606,
"lng" : 21.3122677
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 46.2217118,
"lng" : 21.3823449
},
"southwest" : {
"lat" : 46.1286902,
"lng" : 21.230371
}
}
},
"place_id" : "ChIJw1lco0uYRUcRkYRVPkoHcA8",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}

我的java代码:

jsonString = readUrl("https://maps.googleapis.com/")
rootNode = objectMapper.readTree(jsonString);
JsonNode resultsNode = rootNode.path("results");
JsonNode addressNode = resultsNode.path("address_components");
JsonNode geometryNode = addressNode.path("geometry");
JsonNode locationNode = geometryNode.path("location");
JsonNode latNode = locationNode.path("lat");
JsonNode lngNode = locationNode.path("lng");
System.out.println(latNode.asDouble());
System.out.println(lngNode.asDouble());

最后两行打印 0.0 和 0.0。如何从该 json 中获取 lat 和 lng 信息?p.s 我没有发布完整的网址链接。也许 jsonNodes 不正确,但我找不到解决方案。

最佳答案

问题是 results 不是一个对象,而是一个对象数组。您必须按索引选择元素。正确的解决办法是:

jsonString = readUrl("https://maps.googleapis.com/")
rootNode = objectMapper.readTree(jsonString);
JsonNode resultsNode = rootNode.path("results");
JsonNode addressNode = resultsNode.get(0); //selecting first json object from an array
JsonNode geometryNode = addressNode.path("geometry");
JsonNode locationNode = geometryNode.path("location");
JsonNode latNode = locationNode.path("lat");
JsonNode lngNode = locationNode.path("lng");
System.out.println(latNode.asDouble());
System.out.println(lngNode.asDouble());

您可能还想迭代结果中的所有对象,在这种情况下,将打印每个对象坐标的代码应该是:

jsonString = readUrl("https://maps.googleapis.com/")
rootNode = objectMapper.readTree(jsonString);
JsonNode resultsNode = rootNode.path("results");
Iterator<JsonNode> results = resultsNode.elements();
while(results.hasNext()){
JsonNode addressNode = results.next();
JsonNode geometryNode = addressNode.path("geometry");
JsonNode locationNode = geometryNode.path("location");
JsonNode latNode = locationNode.path("lat");
JsonNode lngNode = locationNode.path("lng");
System.out.println(latNode.asDouble());
System.out.println(lngNode.asDouble());
}

关于java - 我有一个 json 字符串,但无法提取特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39101478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com