将一种 JSON 格式转换为另一种格式
Convert one JSON format to another
我想将下面的 json 格式转换为相应的 json 格式。我从 Rest 服务收到的 Json 不支持 design.I 厌倦了实现下面的列表视图
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 格式,它适用于 JSON 数组长度=1.For 更大的数组长度它不起作用。
这是我的代码
https://jsfiddle.net/72mz5zft/
我认为基本上您要做的是非常简单的映射:
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);
我想将下面的 json 格式转换为相应的 json 格式。我从 Rest 服务收到的 Json 不支持 design.I 厌倦了实现下面的列表视图
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 格式,它适用于 JSON 数组长度=1.For 更大的数组长度它不起作用。 这是我的代码 https://jsfiddle.net/72mz5zft/
我认为基本上您要做的是非常简单的映射:
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);