简体   繁体   English

将一种JSON格式转换为另一种

[英]Convert one JSON format to another

I want to convert one below json format to respective json format. 我想将以下json格式转换为相应的json格式。 The Json I receive from Rest services is not supporting the design.I tired to implement the below list view 我从Rest服务收到的Json不支持该设计,我很疲倦地实现以下列表视图

http://www.oracle.com/webfolder/technetwork/jet/jetCookbook.html?component=listView&demo=jsonHierListView http://www.oracle.com/webfolder/technetwork/jet/jetCookbook.html?component=listView&demo=jsonHierListView

JSON Format (I receive from Rest services) 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 format I need 我需要的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": ""
          }
        }

      ]
    }
  ]
}

I tired to convert the JSON format and it worked well for JSON array of length=1.For greater Array length it is not working. 我厌倦了转换JSON格式,它对于length = 1的JSON数组很有效,对于更大的Array长度则不起作用。 Here is my code https://jsfiddle.net/72mz5zft/ 这是我的代码https://jsfiddle.net/72mz5zft/

I think essentially what you're trying to do is pretty simple mapping: 我认为本质上您想要做的是非常简单的映射:

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM