【问题标题】:reading in jSON data with javascript使用 javascript 读取 JSON 数据
【发布时间】:2012-02-01 23:20:31
【问题描述】:

尝试使用 JS 将 google maps api 中的一些信息读入我的应用程序。但是没有安静让我开始访问返回的 JSON 对象中的信息。 我的JS是……

        var site = "./maps/scripts/reverseGeocode/locale.php";
        var params = "lat="+lat+"&"+"lng="+lng;

        var object = system.functions().ajax(site, params);

            var result = object.address_components[0];

          alert(result);

json对象如下...

  {
    "results" : [
      {
     "address_components" : [
        {
           "long_name" : "24",
           "short_name" : "24",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Carabella St",
           "short_name" : "Carabella St",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Kirribilli",
           "short_name" : "Kirribilli",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "New South Wales",
           "short_name" : "NSW",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Australia",
           "short_name" : "AU",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "2061",
           "short_name" : "2061",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "24 Carabella St, Kirribilli NSW 2061, Australia",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : -33.84886950,
              "lng" : 151.21871730
           },
           "southwest" : {
              "lat" : -33.84960880,
              "lng" : 151.21810640
           }
        },
        "location" : {
           "lat" : -33.84892950,
           "lng" : 151.21817770
        },
        "location_type" : "RANGE_INTERPOLATED",
        "viewport" : {
           "northeast" : {
              "lat" : -33.84789016970850,
              "lng" : 151.2197608302915
           },
           "southwest" : {
              "lat" : -33.85058813029150,
              "lng" : 151.2170628697085
           }
        }
     },
     "types" : [ "street_address" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Kirribilli",
           "short_name" : "Kirribilli",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "New South Wales",
           "short_name" : "NSW",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Australia",
           "short_name" : "AU",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "2061",
           "short_name" : "2061",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "Kirribilli NSW 2061, Australia",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : -33.84245090,
              "lng" : 151.22032150
           },
           "southwest" : {
              "lat" : -33.85270230,
              "lng" : 151.21152060
           }
        },
        "location" : {
           "lat" : -33.84753980000001,
           "lng" : 151.21314540
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : -33.84245090,
              "lng" : 151.22032150
           },
           "southwest" : {
              "lat" : -33.85270230,
              "lng" : 151.21152060
           }
        }
     },
     "types" : [ "locality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "2061",
           "short_name" : "2061",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "New South Wales",
           "short_name" : "NSW",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Australia",
           "short_name" : "AU",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "New South Wales 2061, Australia",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : -33.84234370,
              "lng" : 151.22032150
           },
           "southwest" : {
              "lat" : -33.85270230,
              "lng" : 151.20932150
           }
        },
        "location" : {
           "lat" : -33.84839570,
           "lng" : 151.21461550
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : -33.84234370,
              "lng" : 151.22032150
           },
           "southwest" : {
              "lat" : -33.85270230,
              "lng" : 151.20932150
           }
        }
     },
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "New South Wales",
           "short_name" : "NSW",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "Australia",
           "short_name" : "AU",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "New South Wales, Australia",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : -28.15619210,
              "lng" : 153.63873290
           },
           "southwest" : {
              "lat" : -37.50503180,
              "lng" : 140.99921220
           }
        },
        "location" : {
           "lat" : -31.25321830,
           "lng" : 146.9210990
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : -28.15619210,
              "lng" : 153.63873290
           },
           "southwest" : {
              "lat" : -37.50503180,
              "lng" : 140.99921220
           }
        }
     },
     "types" : [ "administrative_area_level_1", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Australia",
           "short_name" : "AU",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Australia",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : -9.22108360,
              "lng" : 159.10545890
           },
           "southwest" : {
              "lat" : -54.77721850,
              "lng" : 112.92145430
           }
        },
        "location" : {
           "lat" : -25.2743980,
           "lng" : 133.7751360
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : -9.22108360,
              "lng" : 159.10545890
           },
           "southwest" : {
              "lat" : -54.77721850,
              "lng" : 112.92145430
           }
        }
     },
     "types" : [ "country", "political" ]
  }
  ],
  "status" : "OK" 
 }

我正在尝试访问地址组件...但不确定如何正确遍历对象...有人可以帮助我吗?

           "long_name" : "Kirribilli",
           "short_name" : "Kirribilli",

【问题讨论】:

  • 是的,这是我的家庭住址...请不要跟踪我

标签: javascript arrays json parsing google-maps-api-2


【解决方案1】:

应该这样做:

var long_name = object.address_components[2].long_name;
var short_name = object.address_components[2].short_name;

编辑:建议您使用 jquery ajax 代替获取:

var site = "./maps/scripts/reverseGeocode/locale.php";
var params = "lat="+lat+"&"+"lng="+lng;

$.get(site + '/' + params, function(data) {
    var long_name = data.address_components[2].long_name;
    var short_name = data.address_components[2].short_name;
});

【讨论】:

  • 感谢您的及时回复...它返回 object.address_components 未定义。这就是我遇到的问题......它有点好奇有什么想法吗?
  • 感谢 helk,但我试图避免使用 jQuery 处理这类事情。
  • 那么你必须用老式的方式解码 json =) 这是一个教程json.org/js.html
  • 这里有一个很好的不带 jQuery 的 ajax 调用教程:w3schools.com/ajax/ajax_aspphp.asp
猜你喜欢
  • 2013-05-15
  • 2018-01-25
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 2015-08-14
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多