如何迭代这种json?(how to iterate this kind of json?)
[
{
"id": 1,
"name": "John",
"age": 11
},
{
"id": 2,
"name": "Maria",
"age": 12
},
[
{
"id": 1,
"store": "market place",
"phone": 1234567890,
"type": "market"
},
{
"id": 2,
"store": "ration",
"phone": 1234567890,
"type": "animal"
}
]
]
我正在尝试获取store , phone和type值。 但我只得到了name和age 。
我有这个:
$.each(data, function (i, x) { alert(x.phone) // give all null 4x times });我怎么能迭代我所有的json? 我试图添加另一个,但它显示我undefined
[ { "id": 1, "name": "John", "age": 11 }, { "id": 2, "name": "Maria", "age": 12 }, [ { "id": 1, "store": "market place", "phone": 1234567890, "type": "market" }, { "id": 2, "store": "ration", "phone": 1234567890, "type": "animal" } ] ]I'm trying to get the store, phone, and type values. But I only got the name and age.
I have this:
$.each(data, function (i, x) { alert(x.phone) // give all null 4x times });How can I iterate all my json? I tried to add another each but its showing me undefined
最满意答案
那是非常奇怪的数据结构。 问题是你有两种类型,对象和数组的末尾,另一个数组。 因此,要获取商店和电话值,您需要识别此阵列中的数组:
$.each(data, function(i, x) { //Test if item is array if ($.isArray(x)) { //If it is, cycle throught it, find desired data $.each(x, function(i2, x2) { alert(x2.phone) alert(x2.store) }); } });Thats is very wierd datastructure. Problem si that you have two types there, objects and at the end of array, another array. So to get store and phone values you need to identify the array in this array:
$.each(data, function(i, x) { //Test if item is array if ($.isArray(x)) { //If it is, cycle throught it, find desired data $.each(x, function(i2, x2) { alert(x2.phone) alert(x2.store) }); } });更多推荐
发布评论