javascript - console.log(JSON.parse(result)) 在我期望看到 JSON 的地方打印 [Object]

标签 javascript json node.js google-maps-api-3

给定一个看似有效的 JSON 格式的字符串(从 api 调用接收),我如何解析该字符串以便可以访问

JSON.parse(data) 的值返回为包含 [Object] 的 JSON,这对我没有帮助。
我正在尝试从以字符串形式发送给我的数据中获取纬度和经度,我认为可以将其转换为 JSON。
我知道我可以将 data 作为字符串来使用,但我想将其用作 JSON。

{ results: 
   [ { address_components: [Object],
       formatted_address: 'Google Bldg 41, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA',
       geometry: [Object],
       place_id: 'ChIJxQvW8wK6j4AR3ukttGy3w2s',
       types: [Object] } ],
  status: 'OK' }

这是数据的值(value):

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Google Building 41",
               "short_name" : "Google Bldg 41",
               "types" : [ "premise" ]
            },
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Google Bldg 41, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.4228642,
                  "lng" : -122.0851557
               },
               "southwest" : {
                  "lat" : 37.4221145,
                  "lng" : -122.0859841
               }
            },
            "location" : {
               "lat" : 37.4224082,
               "lng" : -122.0856086
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238383302915,
                  "lng" : -122.0842209197085
               },
               "southwest" : {
                  "lat" : 37.4211403697085,
                  "lng" : -122.0869188802915
               }
            }
         },
         "place_id" : "ChIJxQvW8wK6j4AR3ukttGy3w2s",
         "types" : [ "premise" ]
      }
   ],
   "status" : "OK"
}

最佳答案

您可以像这样获取纬度和经度:

parsedData = JSON.parse(data);
lat = parsedData.results[0].geometry.location.lat;
lng = parsedData.results[0].geometry.location.lng;

关于javascript - console.log(JSON.parse(result)) 在我期望看到 JSON 的地方打印 [Object],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44089535/

相关文章:

json - 从数组初始化的可解码

javascript - 设置 i18n 的默认值

java - 使用 Java 正则表达式混淆 JSON 中的密码参数

c# - 发送FIN包,通知数据已发送

node.js - Mongoose 进行身份验证导致超时

javascript - 使用python从网站获取元素,无需打开浏览器

javascript - 如何操作浏览器的后退按钮

javascript - Angular 错误配置错误 : [$resource:badcfg] query

javascript - D3 - 在 DOM 元素之间绘制路径

javascript - 范围过滤器仅在第一个 Controller 中工作