【发布时间】:2014-04-10 23:25:03
【问题描述】:
我正在尝试解析以下 JSON:
{
"customers": [
{ "name":"joe" , "cars":[
{"name":"honda","visits":[
{"date":"01/30/14","Id":"201"},
{"date":"01/30/14","Id":"201"},
{"date":"02/12/14","Id":"109"}
]},
{"name":"Nissan","visits":[
{"date":"01/30/14","Id":"201"},
{"date":"02/12/14","Id":"109"}
]}
]},
{ "name":"bob" , "cars":[
{"name":"escalade","visits":[
{"date":"01/05/14","Id":"301"},
{"date":"01/18/14","Id":"551"}
]},
{"name":"corvette","visits":[
{"date":"01/05/14","Id":"301"},
{"date":"01/18/14","Id":"551"}
]}
]}
]
}
使用以下 jQuery 脚本:
$("document").ready(function(){
$.getJSON("data1.json", function(json) {
console.log(json); // this will show the info it in firebug console
$.each(json.customers,function(customer){
console.log(customer.name);
console.log(customer.cars);
});
});
});
JSON 在控制台中通过,但我尝试解析的字段显示为未定义。谁能告诉我我做错了什么?
【问题讨论】:
-
customer是数组中对象的索引。该对象作为第二个参数传递。请参阅文档:api.jquery.com/jQuery.each。将console.log(customer)放入回调中有助于调试问题。 -
console.log(this.name)和console.log(this.cars)。
标签: javascript jquery json parsing