解析谷歌地图Json文件

时间:2014-07-16 19:48:02

标签: json

我有这个Json文件,我试图用jquery getJSON访问一些参数。

这是Json文件:

{
"results" : [
  {
     "address_components" : [
        {
           "long_name" : "croatia",
           "short_name" : "HR",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "croatia",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 46.5552234,
              "lng" : 19.4480523
           },
           "southwest" : {
              "lat" : 42.3922652,
              "lng" : 13.4896912
           }
        },
        "location" : {
           "lat" : 45.1,
           "lng" : 15.2
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 46.5552234,
              "lng" : 19.4480523
           },
           "southwest" : {
              "lat" : 42.3923464,
              "lng" : 13.4896912
           }
        }
     },
     "types" : [ "country", "political" ]
  }
],
"status" : "OK"
}

这是jquery代码

$.getJSON('SOME JSON FILE', function(data) {
    var output="<ul>";

        output+="<li>" + **data.results[geometry].location.lat** + " " + "</li>";


    output+="</ul>";
    document.getElementById("placeholder").innerHTML=output;

});

我做错了什么,你能帮我查一下location-&gt; lat参数吗?

1 个答案:

答案 0 :(得分:2)

你在** data.results [geometry] .location.lat **中犯了一些错误,这可能会格式错误

您好此代码可以帮助您

 $.getJSON('SOME JSON FILE', function (data) {
            $.each(data.results, function (i, f) {
                var output = "<ul>";
                output += "<li>" + f.geometry.location.lat + " " + "</li>";
                output += "</ul>";
                document.getElementById("placeholder").innerHTML = output;
            });
        });