0

我想将下面的 json 格式转换为相应的 json 格式。我从 Rest 服务收到的 Json 不支持该设计。我厌倦了实现以下列表视图

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

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/

4

1 回答 1

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

于 2017-05-19T09:14:26.677 回答