简体   繁体   English

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

[英]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.

相关问题 如何提取 JSON 文件的特定部分? - How can I extract a specific part of a JSON file? 如何从Java的字符串行中提取特定术语? - How can i extract specific terms from string lines in Java? 如何从字符串行中提取特定术语? - How can i extract specific terms from string lines? 如何使用 Jackson 在 JSON 中解开特定字段? - How can I unwrap a specific field in a JSON using Jackson? 我如何更改字符串字段的值(格式json)? - how i can change value of field of a string (format json)? Java 正则表达式:从字符串“I_INSERT_TO_TOPIC_345674_123456_4.json”中提取特定模式 - Java Regex : Extract a specific pattern from a string “I_INSERT_TO_TOPIC_345674_123456_4.json” 我无法将json字符串转换为List <T> 由GSON - I can't convert json string to List<T> by GSON 我有一个字符串,我想在其中提取亲属关系和姓名 - I have a string in which I want to extract both kinship and Name 我想通过回调 <T> 将String反序列化为JSON,我写了这个,但是我不能使用 - I want through the Callback<T> to deserialize String to JSON , and I write this but i can't use 为什么我不能使用变量从数组(或数组列表)中提取特定值,例如检查以下代码 - Why can't I use a variable to extract a specific value from an array (or arraylist), for example check the following code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM