console.log(JSON.parse(result)) 打印 [对象] 我希望看到 JSON 的位置



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

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

像这样得到纬度和液化天然气:

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

相关内容

最新更新