我想将一个json格式转换为相应的json格式。我从Rest服务收到的Json不支持该设计。我厌倦了实现下面的列表视图
JSON格式(我从Rest服务收到)
{
"ActionHistory": [{
"id": "action",
"name": "Action History",
"children": [{
"childrenItem": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To": "Neena Kochlar",
"pic": "deb_avatar",
"Details": ""
}
}, {
"childrenItem": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To": "James",
"pic": "neena_avatar",
"Details": ""
}
}]
},
{
"id": "action",
"name": "Action History2",
"children": [{
"childrenItem": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To": "Neena Kochlar",
"pic": "deb_avatar",
"Details": ""
}
}, {
"childrenItem": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To": "James",
"pic": "neena_avatar",
"Details": ""
}
}]
}
]
}
我需要的JSON格式
{
"ActionHistory": [
{
"attr": {
"id": "action",
"name": "Action History"
},
"children": [{
"attr": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To": "Neena Kochlar",
"pic": "deb_avatar",
"Details": ""
}
},
{
"attr": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To": "James",
"pic": "neena_avatar",
"Details": ""
}
}
]
}, {
"attr": {
"id": "action",
"name": "Action History"
},
"children": [{
"attr": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To": "Neena Kochlar",
"pic": "deb_avatar",
"Details": ""
}
},
{
"attr": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To": "James",
"pic": "neena_avatar",
"Details": ""
}
}
]
}
]
}
我厌倦了转换JSON格式,它适用于长度为1的JSON数组。对于更大的数组长度,它不起作用。 这是我的代码 https://jsfiddle.net/72mz5zft/
答案 0 :(得分:0)
我认为你基本上想要做的就是非常简单的映射:
var person={"ActionHistory":[
{
"id": "action",
"name": "Action History",
"children": [
{"childrenItem": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To":"Neena Kochlar",
"pic":"deb_avatar",
"Details":""
}
},{"childrenItem": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To":"James",
"pic":"neena_avatar",
"Details":""
}
},
{"childrenItem": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "",
"Action": "Pending",
"From": "James",
"To":"",
"pic":"james_avatar",
"Details":""
}
}
]
}
]};
var result = {
ActionHistory: person.ActionHistory.map(function(item){
return {attr: {
id: item.id,
name: item.name,
children: item.children.map(function(child){
return {
attr: child.childrenItem
}
})
}}
})
};
console.log(result);