【发布时间】:2016-10-29 16:51:13
【问题描述】:
我一直在尝试使用 jquery 使用名称获取 json 数据,但没有成功,仅适用于对象索引,这是我的代码:
var json = [
{
"idiom":[
{
"nombre":"español",
"id":1
},
{
"nombre":"ingles",
"id":2
}
]
}
];
console.log(json)
$.each( json[0], function( key, data ) {
$.each(data, function(index, indata) {
console.log(indata.nombre);
});
});
console.log('-------');
$.each( json.idiom, function( key, data ) {
$.each(data, function(index, indata) {
console.log(indata.nombre);
});
});
如果我尝试使用 json.idiom 而不是 json[0] 访问,则代码不起作用
这是一个小提琴:https://jsfiddle.net/wm0de41h/
【问题讨论】:
-
json.idiom如果你有json = { "idiom": [ ... ] }会工作,但你没有... -
请正确缩进您的代码。
-
我这样说是因为我会添加更多数组,而不仅仅是“成语”,这就是为什么 [ ] 开头的原因,例如json = [ {"idiom": [ ... ]}, {"age": [ ... ]}, {"city": [ ... ]} ]
-
每个对象可以有多个键,你知道吗?
{ "idiom": [...], "age": [...], "city": [...] }等不需要数组。
标签: javascript jquery json