【发布时间】:2015-01-26 17:21:42
【问题描述】:
我正在使用 Javascript/JQuery 来使用 Google Web 服务,并且可以毫无问题地检索 JSON 结果,将它们存储在一个 var(名为 searchResults)中。在此示例中,我只是将要显示的所有信息从对象(address_components 的每个子对象中的 long_name 以及 lat & lng 值)添加到字符串 var (displayTxt)。
但是,结果中有许多未命名的对象,我找不到简单的方法来定位它们。我想使用标准的 object.property 语法,尽管如果没有命名对象(并且有多个),这是不可能的。我已经使用嵌入式 $.each 循环来遍历每个连续的对象,这个解决方案确实有效,但我认为它不是很整洁。所以我的问题是:有没有更好的方法来达到同样的结果?
$( "#displayBtn" ).click(function()
{
var displayTxt = "";
if (searchResults.results)
{
$.each(searchResults.results, function (k,v)
{
displayTxt +=( "Result ID: " + k + "<br>");
$.each(v,function(a,b)
{
if(a=="address_components")
{
$.each(b,function(c,d)
{
displayTxt += (d.long_name + "<br>")
})
}
})
displayTxt+= "lat:" + v.geometry.location.lat +"<br>";
displayTxt+= "lng:" + v.geometry.location.lng +"<br><br>";
}
)
};
$('#resultsDisplay').html(displayTxt);
});
这里是 searchResults 对象:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Reading",
"short_name" : "Reading",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Exeter",
"short_name" : "Exeter",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Berks County",
"short_name" : "Berks County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Pennsylvania",
"short_name" : "PA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Reading, PA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.371134,
"lng" : -75.890512
},
"southwest" : {
"lat" : 40.296401,
"lng" : -75.9598731
}
},
"location" : {
"lat" : 40.3356483,
"lng" : -75.9268747
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.371134,
"lng" : -75.890512
},
"southwest" : {
"lat" : 40.296401,
"lng" : -75.9598731
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Reading",
"short_name" : "Reading",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Reading",
"short_name" : "Reading",
"types" : [ "postal_town" ]
},
{
"long_name" : "Reading",
"short_name" : "Reading",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "England",
"short_name" : "England",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Reading, Reading, Reading, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.4931339,
"lng" : -0.9284944000000001
},
"southwest" : {
"lat" : 51.4097795,
"lng" : -1.0636011
}
},
"location" : {
"lat" : 51.4542645,
"lng" : -0.9781303
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.4931339,
"lng" : -0.9284944000000001
},
"southwest" : {
"lat" : 51.4097795,
"lng" : -1.0636011
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Reading",
"short_name" : "Reading",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Middlesex County",
"short_name" : "Middlesex County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Massachusetts",
"short_name" : "MA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Reading, MA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 42.566432,
"lng" : -71.07103499999999
},
"southwest" : {
"lat" : 42.5006449,
"lng" : -71.1361789
}
},
"location" : {
"lat" : 42.5256563,
"lng" : -71.0952891
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 42.566432,
"lng" : -71.07103499999999
},
"southwest" : {
"lat" : 42.5006449,
"lng" : -71.1361789
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Reading",
"short_name" : "Reading",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Hamilton County",
"short_name" : "Hamilton County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Ohio",
"short_name" : "OH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Reading, OH, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 39.240921,
"lng" : -84.407549
},
"southwest" : {
"lat" : 39.2038819,
"lng" : -84.454735
}
},
"location" : {
"lat" : 39.2236694,
"lng" : -84.44216410000001
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 39.240921,
"lng" : -84.407549
},
"southwest" : {
"lat" : 39.2038819,
"lng" : -84.454735
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Reading",
"short_name" : "Reading",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Windsor County",
"short_name" : "Windsor County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Vermont",
"short_name" : "VT",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Reading, VT, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 43.561169,
"lng" : -72.53007579999999
},
"southwest" : {
"lat" : 43.4440849,
"lng" : -72.66064299999999
}
},
"location" : {
"lat" : 43.494167,
"lng" : -72.59638900000002
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 43.561169,
"lng" : -72.53007579999999
},
"southwest" : {
"lat" : 43.4440849,
"lng" : -72.66064299999999
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Reading",
"short_name" : "Reading",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Summit Lake",
"short_name" : "Summit Lake",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Nobles County",
"short_name" : "Nobles County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Minnesota",
"short_name" : "MN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "56165",
"short_name" : "56165",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Reading, MN 56165, USA",
"geometry" : {
"location" : {
"lat" : 43.7035752,
"lng" : -95.7130676
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 43.70492418029149,
"lng" : -95.71171861970849
},
"southwest" : {
"lat" : 43.70222621970849,
"lng" : -95.71441658029151
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
【问题讨论】:
-
“未命名的对象”是什么意思?听起来您确实在谈论使用数组元素...
-
结果包含对象数组,因此您只能遍历这些数组。
-
事后看来,我不得不同意。 JavaScript 新手。
标签: javascript jquery