从 JSON 结果中获取 postal_code 值

发布于 2024-12-20 22:55:32 字数 1339 浏览 4 评论 0原文

我正在使用 Google 地图地理编码器。我已经一切正常,但我似乎无法弄清楚如何“遍历”(解析?)JSON 结果。

如何从地理编码器的 JSON 结果中获取邮政编码?

我的尝试是循环遍历“address_components”,测试包含“postal_code”的数组的每个“values”键。

所以这是我到目前为止所写的内容的片段:

var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address : cAddress }, function(results, status) {
    if(status == google.maps.GeocoderStatus.OK) {
        if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
            var fAddress = results[0].formatted_address;
        var contactLatLng = results[0].geometry.location;

        var postalCode = $.each(results[0].address_components, 
                function(componentIndex, componentValue) {
                    var typesArray = componentValue.types;
            if ($.inArray("postal_code", typesArray)) {
                return componentValue.long_name;
                    }
            })
        }
    }
});

问题具体是 postalCode

[object Object],[object Object],[object Object],[object Object],  
[object Object],[object Object],[object Object]`

显然,我遗漏了一些东西。

作为参考,以下是 Google Maps Geocoder JSON 结果的链接: http://code.google.com/apis/maps/documentation/geocoding/ #JSON

感谢您的帮助! ~阿莫斯

I'm working with the Google Maps Geocoder. I've got everything working fine, but I can't seem to figure out how to 'traverse' (parse?) the JSON results.

How can I get the postal code from the Geocoder's JSON results?

My attempt as been to loop through 'address_components', testing each "values" key for an array containing "postal_code".

So here's a snippet of what I've written so far:

var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address : cAddress }, function(results, status) {
    if(status == google.maps.GeocoderStatus.OK) {
        if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
            var fAddress = results[0].formatted_address;
        var contactLatLng = results[0].geometry.location;

        var postalCode = $.each(results[0].address_components, 
                function(componentIndex, componentValue) {
                    var typesArray = componentValue.types;
            if ($.inArray("postal_code", typesArray)) {
                return componentValue.long_name;
                    }
            })
        }
    }
});

The problem specifically is postalCode is

[object Object],[object Object],[object Object],[object Object],  
[object Object],[object Object],[object Object]`

Obviously, there is something I'm missing.

For reference, here is the link to Google Maps Geocoder JSON results:
http://code.google.com/apis/maps/documentation/geocoding/#JSON

Thanks for any help!
~Amos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软糯酥胸 2024-12-27 22:55:32

另请注意,“返回”不起作用。这是一个异步函数。因此,当您的函数运行时,父函数已经完成。

$.each(results[0].address_components, function(componentIndex, componentValue) {
     if ($.inArray("postal_code", componentValue.types)) {
           doSomeThingWithPostcode(componentValue.long_name);
     }
});

所以你的函数必须明确地对结果做一些事情。例如...

function doSomeThingWithPostcode(postcode) {
     $('#input').attr('value',postcode);
}

Also note, "return" doesn't work. It's a asynchronous function. So by the time your function runs, the parent function has finished.

$.each(results[0].address_components, function(componentIndex, componentValue) {
     if ($.inArray("postal_code", componentValue.types)) {
           doSomeThingWithPostcode(componentValue.long_name);
     }
});

So your function must do something explicitly with the result. for example...

function doSomeThingWithPostcode(postcode) {
     $('#input').attr('value',postcode);
}
眸中客 2024-12-27 22:55:32

假设这里的 $ 是 jQuery 对象,您将返回 results[0].address_components 集合,因为您的 return componentValue.long_name; 被忽略通过each()。您正在寻找的是 $.map(),它将返回修改后的集合。

Assuming $ here is jQuery object, you are geting back the results[0].address_components collection, since your return componentValue.long_name; is ignored by each(). What you are looking for is $.map(), which will return modified collection.

清浅ˋ旧时光 2024-12-27 22:55:32

首先,我要对此表示感谢。刚刚帮我自己摆脱了困境。然而,我确实必须稍微改变一下代码。

我遇到的问题是 jQuery.inArray() 不返回布尔值 - 它要么返回数组中元素的索引,要么返回 -1。我对此感到困惑,如果不更改 if 语句来读取,我就无法让您的代码工作:

if( $.inArray( "postal_code", typesArray ) != -1 ) {
    pc =  componentValue.long_name;
}

当我将其设置为检查 true 或 false 时,该 if 块中的代码将在每次迭代中运行$.each() 循环的一部分,因为 if 语句总是返回 -1 而不是 0 或 false。检查 $.inArray() 方法是否返回 -1 后,代码运行顺利。

再次感谢!

Firstly, let me say thanks for this. Just helped me out of a jam myself. I did, however, have to alter the code a little bit.

The issue I had was that jQuery.inArray() does not return a boolean value - it either returns the element's index in the array or -1. I got confused with this, and I couldn't get your code to work without changing the if statement to read as such:

if( $.inArray( "postal_code", typesArray ) != -1 ) {
    pc =  componentValue.long_name;
}

When I had it set to check for true or false, the code within that if block would run on every single iteration of the $.each() loop, because the if statement would always return -1 instead of 0 or false. After checking if the $.inArray() method returned -1 or not, the code worked swimmingly.

Thanks again!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文