我有一个 JSON文件,我必须在页面加载时从该文件中读取数据.我怀疑我的JSON结构有问题. JSONLint显示它是有效的.所以我必须使用错误的方法来访问它. 它基本上是一个对象数组(或者我认为
它基本上是一个对象数组(或者我认为).
{"Listings":[
{"Listing1":
{
"agency_code":"BP",
"property_code":"BON1",
"Property_GUID":"6dded624",
"FileNo /":"",
"country":"AUSTRALIA",
"state":"New South Wales",
"subregion /":""
}
},
{"Listing1":
{
"agency_code":"BPGA",
"property_code":"BONNSTG4-Lot11",
"Property_GUID":"6dded624-cde2-429a-81d4-bd6f91256345",
"FileNo /":"",
"country":"AUSTRALIA",
"state":"New South Wales",
"subregion /":""
}
}
]
}
我使用$.ajax来读取JSON.文件加载成功.现在我如何访问单个“列表”以及如何衡量总共有多少个列表?
我尝试使用$.each循环遍历数组,但我的代码无效.
$.ajax({
dataType : 'json',
success : function (response) {
for (var i = 0, len = response.Listings.length; i < len; i++) {
//You can now access individual properties like this:
var agencyCode = response.Listings[i].Listing1.agency_code;
}
}
});
这个for循环的执行速度比jQuery的.each()或$.each():http://jsperf.com/jquery-each-vs-for-loops/2快
这是一个演示:http://jsfiddle.net/btHy5/1/
