Javascript 如何解析json数组并获得预期结果?
我正在解析json。我正在检查json数组,然后再创建另一个对象。我只是在一个地方敲了一下。实际上,我正在检查,如果父对象有子对象,我会在“testCaseList”数组中添加一个与子对象相对应的对象。但我需要检查子id是否有字符“not”,它应该在这个数组“commandList”中添加字符 这是我的意见Javascript 如何解析json数组并获得预期结果?,javascript,jquery,arrays,json,Javascript,Jquery,Arrays,Json,我正在解析json。我正在检查json数组,然后再创建另一个对象。我只是在一个地方敲了一下。实际上,我正在检查,如果父对象有子对象,我会在“testCaseList”数组中添加一个与子对象相对应的对象。但我需要检查子id是否有字符“not”,它应该在这个数组“commandList”中添加字符 这是我的意见 [ { "id": "a", "text": "a", "icon": true, "li_attr": { "id": "a" },
[
{
"id": "a",
"text": "a",
"icon": true,
"li_attr": {
"id": "a"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
},
{
"id": "b",
"text": "b\n ",
"icon": true,
"li_attr": {
"id": "b"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
{
"id": "b-a-1",
"text": "b-a",
"icon": true,
"li_attr": {
"id": "b-a-1"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
},
{
"id": "b-b-2",
"text": "b-b\n ",
"icon": true,
"li_attr": {
"id": "b-b-2"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
{
"id": "b-b-a",
"text": "b-b-a",
"icon": true,
"li_attr": {
"id": "b-b-a"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
},
{
"id": "b-b-b",
"text": "b-b-b",
"icon": true,
"li_attr": {
"id": "b-b-b"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
}
]
}
]
},
{
"id": "c-1",
"text": "c\n ",
"icon": true,
"li_attr": {
"id": "c-1"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
{
"id": "not-c-a-1",
"text": "c-a",
"icon": true,
"li_attr": {
"id": "not-c-a-1"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
},
{
"id": "not-c-b-2",
"text": "b-b",
"icon": true,
"li_attr": {
"id": "not-c-b-2"
},
"a_attr": {
"href": "#"
},
"state": {
"loaded": true,
"opened": false,
"selected": false,
"disabled": false
},
"data": {
},
"children": [
]
}
]
}
]
出去把这个放出去
[
{
"a": {
"commandList": [],
"testCaseList": []
}
},
{
"b": {
"commandList": [],
"testCaseList": [
{
"b-a-1": {
"commandList": [],
"testCaseList": []
}
},
{
"b-b-2": {
"commandList": [],
"testCaseList": [
{
"b-b-a": {
"commandList": [],
"testCaseList": []
}
},
{
"b-b-b": {
"commandList": [],
"testCaseList": []
}
}
]
}
}
]
}
},
{
"c-1": {
"commandList": [],
"testCaseList": [
{
"not-c-a-1": {
"commandList": [],
"testCaseList": []
}
},
{
"not-c-b-2": {
"commandList": [],
"testCaseList": []
}
}
]
}
}
]
预计产量为:
[
此mapItem函数将执行您需要的操作:
function mapItem(inputItem) {
var item = {};
item[inputItem.id] = JSON.parse(sessionStorage.getItem(inputItem.id));
for (k in inputItem.children) {
if (/^not-/.test(inputItem.children[k].id)) {
item[inputItem.id].commandList.push(mapItem(inputItem.children[k]));
}else{
item[inputItem.id].testCaseList.push(mapItem(inputItem.children[k]));
}
}
return item;
}
你的密码在哪里。把你的密码贴在这里。这是小提琴
function mapItem(inputItem) {
var item = {};
item[inputItem.id] = JSON.parse(sessionStorage.getItem(inputItem.id));
for (k in inputItem.children) {
if (/^not-/.test(inputItem.children[k].id)) {
item[inputItem.id].commandList.push(mapItem(inputItem.children[k]));
}else{
item[inputItem.id].testCaseList.push(mapItem(inputItem.children[k]));
}
}
return item;
}