我需要从json文件下面打印postal_code值,
{
"results" : [
{
"address_components" : [
{
"long_name" : "286",
"short_name" : "286",
"types" : [ "street_number" ]
},
{
"long_name" : "West El Camino Real",
"short_name" : "W El Camino Real",
"types" : [ "route" ]
},
{
"long_name" : "Old Mountain View",
"short_name" : "Old Mountain View",
"types" : [ "neighborhood", "political" ]
},
{
"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" : "94040",
"short_name" : "94040",
"types" : [ "postal_code" ]
},
{
"long_name" : "2606",
"short_name" : "2606",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "286 W El Camino Real, Mountain View, CA 94040, USA",
"geometry" : {
"location" : {
"lat" : 37.3833211,
"lng" : -122.0782706
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.38467008029149,
"lng" : -122.0769216197085
},
"southwest" : {
"lat" : 37.3819721197085,
"lng" : -122.0796195802915
}
}
},
"place_id" : "ChIJieTN_yu3j4AR6cF-aEhdc58",
"types" : [ "street_address" ]
}
"status" : "OK"
}
我已编写以下代码以获取和打印响应。
$.ajax({
dataType: "json",
url: 'https://maps.googleapis.com/maps/api/geocode/json?latlng=37.383253,-122.078075&sensor=false',
data: latlog,
success: function (response) {
$("#response").html(JSON.stringify(response["results"], null, 4));
}
});
这会打印几乎整个对象。如何只打印postal_code值94040?
答案 0 :(得分:0)
您的JSON错误,请查看演示代码以查看正确的语法。
如何只打印postal_code值94040?
您需要访问地址组件并过滤掉类型为postal_code
试试这个
obj.results[0].address_components.filter( function(obj){
return obj.types.indexOf("postal_code") != -1
})[0].long_name
<强>样本强>
var obj = {
"results" : [
{
"address_components" : [
{
"long_name" : "286",
"short_name" : "286",
"types" : [ "street_number" ]
},
{
"long_name" : "West El Camino Real",
"short_name" : "W El Camino Real",
"types" : [ "route" ]
},
{
"long_name" : "Old Mountain View",
"short_name" : "Old Mountain View",
"types" : [ "neighborhood", "political" ]
},
{
"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" : "94040",
"short_name" : "94040",
"types" : [ "postal_code" ]
},
{
"long_name" : "2606",
"short_name" : "2606",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "286 W El Camino Real, Mountain View, CA 94040, USA",
"geometry" : {
"location" : {
"lat" : 37.3833211,
"lng" : -122.0782706
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.38467008029149,
"lng" : -122.0769216197085
},
"southwest" : {
"lat" : 37.3819721197085,
"lng" : -122.0796195802915
}
}
},
"place_id" : "ChIJieTN_yu3j4AR6cF-aEhdc58",
"types" : [ "street_address" ]
}],
"status" : "OK"
}
var long_name = obj.results[0].address_components.filter( function(obj){ return obj.types.indexOf("postal_code") != -1 } )[0].long_name;
console.log(long_name);
&#13;