【问题标题】:Reading JSON data from Google Geocoding API with jQuery使用 jQuery 从 Google Geocoding API 读取 JSON 数据
【发布时间】:2016-07-30 01:10:16
【问题描述】:

我正在尝试了解 Google 地图地理编码以及如何读取它发回的 JSON 数据。这是 Google 发回的内容:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "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" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224764,
               "lng" : -122.0842499
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238253802915,
                  "lng" : -122.0829009197085
               },
               "southwest" : {
                  "lat" : 37.4211274197085,
                  "lng" : -122.0855988802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

我收到此代码的响应:

$("#submit").click(function(event) {
  var address = encodeURIComponent($("#location").val());

  $.ajax({
    type: "GET",
    url: "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false&key=" + API_KEY,
    dataType: "json",
    success: processJSON
  });

  function processJSON(json) {
    // Do stuff here
  }
});

问题是我不知道在processJSON 里面放什么。我在这里阅读教程:https://www.sitepoint.com/ajaxjquery-getjson-simple-example/.getJSON() 上的文档http://api.jquery.com/jquery.getjson/。但是,我无法理解这两个网站。

如果我想获得postal_code,我将如何读取数据?我试过这个:

function processJSON(json) {
    // Do stuff here
    alert("Postal Code:" + json.address_components[6].long_name);
  }

我在getJSON() 之后也尝试过success

$.getJSON(url, function(json) {
  alert("Postal Code:" + json.address_components[6].long_name);
 });

但是,它们都不会提醒任何东西。我究竟做错了什么?提前致谢!

【问题讨论】:

    标签: javascript jquery json google-maps google-geocoding-api


    【解决方案1】:

    在你ajax之后添加成功函数

    $("#submit").click(function(event) {
      var address = encodeURIComponent($("#location").val());
    
      $.ajax({
        type: "GET",
        url: "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false&key=" + API_KEY,
        dataType: "json",
        success: processJSON
      }).success(function(data){
        processJSON(data);
      }
    
      function processJSON(json) {
        // Do stuff here
        console.log(json);
        alert("Postal Code:" + json.results[0].address_components[6].long_name);
      }
    });
    

    【讨论】:

    • 谢谢,让它进入函数;但是,它说 address_components 未定义。
    • json.results.address_components[6].long_name
    • 同样的错误,但现在出现在json.results(未定义)。
    • 你可以在processJSON函数中console.log json看看到底是什么。
    • 实际上,您根本不应该依赖json.results[0].address_components[6] 成为postal_code。相反,您应该遍历所有json.results[] 并采用postal_code 在其json.results[i].types 中的那个
    【解决方案2】:

    您需要解析成功函数的数据。

    function processJSON(data) {
        var json = JSON.parse(data);
       //..
    }
    

    同时在你的 $.ajax 中添加一个错误函数

    $.ajax({ 
        type..
        url...
        success..
        error: function() {
            console.log('error on request')
        }
        ...
    

    【讨论】:

    • 我收到此错误:JSON.parse: unexpected character at line 1 column 2 of the JSON data.
    • 感谢告知错误函数!
    • 尝试使用 chrome rest 客户端插件查看查询结果。 chrome商店里有一些。您可能没有收到 json。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多