如何迭代这种json?(how to iterate this kind of json?)

编程入门 行业动态 更新时间:2024-10-04 11:18:49
如何迭代这种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) }); } });

更多推荐

本文发布于:2023-07-09 10:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:迭代   json   kind   iterate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!