[英]I have a json string and I can't extract specific field
I Have a json String that looks like this: 我有一个看起来像这样的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"
}
My java code: 我的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());
The last 2 line print 0.0 and 0.0. 最后两行打印0.0和0.0。 How can I obtain lat and lng information from this json? 如何从此json获取经纬度信息? ps I diden't post the full url link. ps我没有张贴完整的URL链接。 Maybe the jsonNodes are not correct but I can't find the solution. 也许jsonNodes不正确,但是我找不到解决方案。
The problem is that results
is not an object but an array of objects. 问题在于results
不是对象而是对象数组。 You have to choose element by index. 您必须按索引选择元素。 The proper solution is: 正确的解决方案是:
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());
You may also want to iterate all objects in results, in that case code, which will print coordinates of each object, should be: 您可能还需要迭代结果中的所有对象,在这种情况下,将打印每个对象的坐标的代码应为:
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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.