console.log(JSON.parse(result))打印[Object]我期望看到JSON

时间:2017-05-20 18:38:28

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

如果字符串看起来是有效的JSON格式(从api调用接收),我该如何解析该字符串以便我可以访问

JSON.parse(data)的值返回为包含[Object]的JSON,这对我没有帮助。
我正在尝试将data中的lat和lng作为字符串发送给我,我认为可以转换为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' }

以下是数据的价值:

{
   "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"
}

1 个答案:

答案 0 :(得分:1)

你可以像这样得到lat和lng:

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