Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中JSON的解析与提取_Javascript_Json_Parsing_Geocoding_Google Geocoder - Fatal编程技术网

Javascript中JSON的解析与提取

Javascript中JSON的解析与提取,javascript,json,parsing,geocoding,google-geocoder,Javascript,Json,Parsing,Geocoding,Google Geocoder,我正在使用谷歌地理编码API,并试图获得以下值: 9.081999,以及 8.6752769999999分别在下面的Google geocoding中使用Javascript对JSON数据进行编码 { "results" : [ { "address_components" : [ { "long_name" : "Nigeria", "short_name" : "NG",

我正在使用谷歌地理编码API,并试图获得以下值: 9.081999,以及 8.6752769999999分别在下面的Google geocoding中使用Javascript对JSON数据进行编码

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Nigeria",
               "short_name" : "NG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Nigeria",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 13.8856449,
                  "lng" : 14.677982
               },
               "southwest" : {
                  "lat" : 4.2464428,
                  "lng" : 2.676932
               }
            },
            "location" : {
               "lat" : 9.081999,
               "lng" : 8.675276999999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 13.8856449,
                  "lng" : 14.677982
               },
               "southwest" : {
                  "lat" : 4.2464428,
                  "lng" : 2.676932
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJDY2kfa8LThARyAvFaEH-qJk",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}
但当我这么做的时候:

var myJSONResult = $.parseJSON(request.responseText);
var myAddress = Array();
for (i = 0; i < myJSONResult.results.length; i++) {
        myAddress[i] = myJSONResult.results[i].geometry.location;
}
var myJSONResult=$.parseJSON(request.responseText);
var myAddress=Array();
对于(i=0;i
它返回未定义的

感谢您的帮助, 谢谢。

试试这个

var myJSONResult = request.responseText;
var myAddress = Array();
for (i = 0; i < myJSONResult.results.length; i++) {
        myAddress[i] = myJSONResult.results[i].geometry.location;
}
var myJSONResult=request.responseText;
var myAddress=Array();
对于(i=0;i

无需使用
$.parseJSON()
,因为您的响应已经是一个JSON对象。

results[i]
已经是
几何体了
,即使如此,
results[i]
仍然给出了
未定义的
为什么要使用$.parseJSON()?只需尝试var myJSONResult=request。responseText@Yogeshrequest.responseText包含整个JSON数据,我只需要
“location”中的值:{“lat”:9.081999,“lng”:8.675276999999}
尝试替换var myJSONResult=$.parseJSON(request.responseText);使用var myJSONResult=request.responseText;