JavaScript JSON 多级迭代解析
JavaScript JSON multilevel iterative parsing
{
"id":0,
"item":[
{
"id":"0-",
"text":"BlueWing",
"userdata":[
{
"name":"cid",
"content":"10377"
}
],
"item":[
{
"id":"0-0",
"text":"Auto",
"userdata":[
{
"name":"cid",
"content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388,10533"
}
],
"item":[
{
"id":"0-0-0",
"text":"Auto",
"userdata":[
{
"name":"cid",
"content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
}
],
"item":[
{
"id":"0-0-0-0",
"text":"testaccount",
"userdata":[
{
"name":"cid",
"content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
}
],
"item":[
{
"id":"0-0-0-0-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
}
]
},
{
"id":"0-0-0-0-1",
"text":"testaccount",
"userdata":[
{
"name":"cid",
"content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
}
],
"item":[
{
"id":"0-0-0-0-1-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
}
]
},
{
"id":"0-0-0-0-1-1",
"text":"testaccount - 10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388",
"userdata":[
{
"name":"cid",
"content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
}
],
"item":[
{
"id":"0-0-0-0-1-1-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388"
}
]
}
]
}
]
}
]
}
]
},
{
"id":"0-0-1",
"text":"Balise Toyota",
"userdata":[
{
"name":"cid",
"content":"10533"
}
],
"item":[
{
"id":"0-0-1-0",
"text":"Balise Toyota",
"userdata":[
{
"name":"cid",
"content":"10533"
}
],
"item":[
{
"id":"0-0-1-0-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10533"
}
]
},
{
"id":"0-0-1-0-1",
"text":"Balise Toyota",
"userdata":[
{
"name":"cid",
"content":"10533"
}
],
"item":[
{
"id":"0-0-1-0-1-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10533"
}
]
},
{
"id":"0-0-1-0-1-1",
"text":"Balise Toyota - 10533",
"userdata":[
{
"name":"cid",
"content":"10533"
}
],
"item":[
{
"id":"0-0-1-0-1-1-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10533"
}
]
},
{
"id":"0-0-1-0-1-1-1",
"text":"Balise.List - 11949",
"userdata":[
{
"name":"clid",
"content":11949
},
{
"name":"cid",
"content":"10533"
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"id":"0-1",
"text":"BlueWing",
"userdata":[
{
"name":"cid",
"content":"10667,10668"
}
],
"item":[
{
"id":"0-1-0",
"text":"HVAC",
"userdata":[
{
"name":"cid",
"content":"10667,10668,10348,10294,10297,10313,10521,10351,10309,10296,10316,10315,10312,10301,10347,10300,10306,10308,10307,10298,10305,10371,10291,10349,10311,10354,10345,10310,10292,10372,10295,10304,10303,10299,10314,10290,10490,10293,10302"
}
],
"item":[
{
"id":"0-1-0-0",
"text":"HVAC",
"userdata":[
{
"name":"cid",
"content":"10667,10668"
}
],
"item":[
{
"id":"0-1-0-0-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10667,10668"
}
]
},
{
"id":"0-1-0-0-1",
"text":"Mann Plumbing & Heating",
"userdata":[
{
"name":"cid",
"content":"10667"
}
],
"item":[
{
"id":"0-1-0-0-1-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10667"
}
]
},
{
"id":"0-1-0-0-1-1",
"text":"Mann Plumbing & Heating - 10667",
"userdata":[
{
"name":"cid",
"content":"10667"
}
],
"item":[
{
"id":"0-1-0-0-1-1-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10667"
}
]
},
{
"id":"0-1-0-0-1-1-1",
"text":"Radius List - 12960",
"userdata":[
{
"name":"clid",
"content":12960
},
{
"name":"cid",
"content":"10667"
}
]
}
]
}
]
},
{
"id":"0-1-0-0-2",
"text":"RES Air Conditioning",
"userdata":[
{
"name":"cid",
"content":"10668"
}
],
"item":[
{
"id":"0-1-0-0-2-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10668"
}
]
},
{
"id":"0-1-0-0-2-1",
"text":"RES Air Conditioning - 10668",
"userdata":[
{
"name":"cid",
"content":"10668"
}
],
"item":[
{
"id":"0-1-0-0-2-1-0",
"text":"Add New List",
"userdata":[
{
"name":"clid",
"content":99999
},
{
"name":"cid",
"content":"10668"
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
]
}
我需要遍历所有级别并按"id"匹配并获取"内容"的值。
var id = "0-1-0-0-0";
如果 JSON id
与 id 匹配,则返回 content
的值。应该返回10667,10668
提前感谢!
基本上,您可以遍历对象中的所有元素,如果找到所需的id
,则停止迭代。
这个问题的问题是第一级的半级,id
像1
(0级(,1-
(1级(和1-0
(2级(以及下一个级别都用破折号隔开。
此建议使用 String#indexOf
并检查路径,所需索引为 0
,并迭代该检查是否为真。如果找到完整路径,则返回对象。如果未找到,则继续迭代和递归。
function getObject(object, path) {
function getPart(o) {
if (!path.indexOf(o.id)) {
if (path === o.id) {
result = o;
return true;
}
return Array.isArray(o.item) && o.item.some(getPart);
}
return false;
}
var parts = path.split('-'),
result;
getPart(object);
return result;
}
var object = { "id": 0, "item": [{ "id": "0-", "text": "BlueWing", "userdata": [{ "name": "cid", "content": "10377" }], "item": [{ "id": "0-0", "text": "Auto", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388,10533" }], "item": [{ "id": "0-0-0", "text": "Auto", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0", "text": "testaccount", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }, { "id": "0-0-0-0-1", "text": "testaccount", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }, { "id": "0-0-0-0-1-1", "text": "testaccount - 10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388", "userdata": [{ "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }], "item": [{ "id": "0-0-0-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10377,10376,10378,10387,10379,10384,10386,10380,10383,10385,10381,10388" }] }] }] }] }] }, { "id": "0-0-1", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1", "text": "Balise Toyota", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1-1", "text": "Balise Toyota - 10533", "userdata": [{ "name": "cid", "content": "10533" }], "item": [{ "id": "0-0-1-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10533" }] }, { "id": "0-0-1-0-1-1-1", "text": "Balise.List - 11949", "userdata": [{ "name": "clid", "content": 11949 }, { "name": "cid", "content": "10533" }] }] }] }] }] }] }, { "id": "0-1", "text": "BlueWing", "userdata": [{ "name": "cid", "content": "10667,10668" }], "item": [{ "id": "0-1-0", "text": "HVAC", "userdata": [{ "name": "cid", "content": "10667,10668,10348,10294,10297,10313,10521,10351,10309,10296,10316,10315,10312,10301,10347,10300,10306,10308,10307,10298,10305,10371,10291,10349,10311,10354,10345,10310,10292,10372,10295,10304,10303,10299,10314,10290,10490,10293,10302" }], "item": [{ "id": "0-1-0-0", "text": "HVAC", "userdata": [{ "name": "cid", "content": "10667,10668" }], "item": [{ "id": "0-1-0-0-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667,10668" }] }, { "id": "0-1-0-0-1", "text": "Mann Plumbing & Heating", "userdata": [{ "name": "cid", "content": "10667" }], "item": [{ "id": "0-1-0-0-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667" }] }, { "id": "0-1-0-0-1-1", "text": "Mann Plumbing & Heating - 10667", "userdata": [{ "name": "cid", "content": "10667" }], "item": [{ "id": "0-1-0-0-1-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10667" }] }, { "id": "0-1-0-0-1-1-1", "text": "Radius List - 12960", "userdata": [{ "name": "clid", "content": 12960 }, { "name": "cid", "content": "10667" }] }] }] }, { "id": "0-1-0-0-2", "text": "RES Air Conditioning", "userdata": [{ "name": "cid", "content": "10668" }], "item": [{ "id": "0-1-0-0-2-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10668" }] }, { "id": "0-1-0-0-2-1", "text": "RES Air Conditioning - 10668", "userdata": [{ "name": "cid", "content": "10668" }], "item": [{ "id": "0-1-0-0-2-1-0", "text": "Add New List", "userdata": [{ "name": "clid", "content": 99999 }, { "name": "cid", "content": "10668" }] }] }] }] }] }] }] }] },
id = "0-1-0-0-0",
part = getObject(object, id);
document.write('<pre>' + JSON.stringify(part, 0, 4) + '</pre>');
相关文章:
- 如何在javascript中迭代数字列表
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 如何迭代Array.prototype函数
- 如何使用jquery迭代具有相同属性的html元素并查找onclick事件
- 在ejs-partial中对JSON对象进行迭代
- 如何在DataTables 2.1中迭代对象数组
- 使用递归属性迭代保留属性结构
- 正在停止.在jquery中的特定时间间隔内,每次迭代的每次执行
- 如果30秒未单击,请应用CSS一次,将其删除,然后重新迭代
- 主干-从模板中迭代的集合中获取特定的模型
- 创建一个方法,通过一个窗口进行迭代并获取Titanium中的所有控件
- 什么's是在javascript中迭代项的最佳方式
- 为什么这只是迭代 HTMLCollection 的奇怪元素
- 是否“;对于的“;循环迭代遵循JavaScript中的数组顺序
- Babel编译错误:找不到模块核心js/library/fn/get迭代器
- 在es6中,将带有回调的事件侦听器设置为可迭代的
- jQuery-迭代不正确?(太长,无法执行)
- 每个$.,循环获胜't逐个迭代HTML元素
- 如何在Jquery中迭代JSON数组
- JavaScript JSON 多级迭代解析