通过jQuery将JSON转换为特定的JSON格式

时间:2018-07-05 12:54:41

标签: jquery json

这是我的JSON输入。我想在每个对象之前附加数据对象,并转换成下面给出的特定格式。同样所有可用的插件都可以使用。将JSON转换为特定格式

  [  
    {  
  "guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
  "companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
  "parentLocationGuid":null,
  "name":"location1",
  "description":"test",
  "isActive":true,
  "row_num":2,
  "children":[  
     {  
        "guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE1",
        "companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
        "parentLocationGuid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
        "name":"child location",
        "description":"test child",
        "isActive":true,
        "row_num":1,
        "children":[  

        ]
     }
  ]
  }
  ]

我想要输出为:

   [  
    {  
  "data":{  
     "guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
     "companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
     "parentLocationGuid":null,
     "name":"location1",
     "description":"test",
     "isActive":true,
     "row_num":2
  },
  "children":[  
     {  
        "data":{  
           "guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE1",
           "companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
           "parentLocationGuid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
           "name":"child location",
           "description":"test child",
           "isActive":true,
           "row_num":1
        },
        "children":[  

        ]
     }
  ]
}
]

2 个答案:

答案 0 :(得分:1)

这是您要制作的ES6实现。这适用于包含多个元素的列表。

function formatter(input){
    let output = new Array();
    input.forEach( data_input=>{
      let result = {};
      let children = [];
      result['data'] = {};
      Object.keys(data_input).forEach(key => {
       if (data_input[key] instanceof Object ){
         if(!(data_input[key] instanceof Array && data_input[key].length===0 )) 
         children = data_input[key];
       }
       else{
        result['data'][key] = data_input[key]
       }
    })
      result['children'] = formatter(children);
      output.push({'data': result['data']},{'children': result['children']})
    })
 return output
}

let myInput = [  
    {  
  "guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
  "companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
  "parentLocationGuid":null,
  "name":"location1",
  "description":"test",
  "isActive":true,
  "row_num":2,
  "children":[  
     {  
        "guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE1",
        "companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
        "parentLocationGuid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
        "name":"child location",
        "description":"test child",
        "isActive":true,
        "row_num":1,
        "children":[  

        ]
     }
  ]
  }
  ]



console.log(formatter(myInput))

答案 1 :(得分:0)

使用ES6解构和传播运算符非常容易

const res = data.map(({children,...otherProps})=>({data:{...otherProps}, children}));

console.log(res)
.as-console-wrapper {max-height: 100%!important;}
<script>
  var data = [{
    "guid": "6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
    "companyGuid": "12A5CD86-F6C6-455F-B27A-EFE587ED410D",
    "parentLocationGuid": null,
    "name": "location1",
    "description": "test",
    "isActive": true,
    "row_num": 2,
    "children": [{
      "guid": "6FCBBB6B-4B5E-42C4-B108-12F594DABEE1",
      "companyGuid": "12A5CD86-F6C6-455F-B27A-EFE587ED410D",
      "parentLocationGuid": "6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
      "name": "child location",
      "description": "test child",
      "isActive": true,
      "row_num": 1,
      "children": [

      ]
    }]
  }]
</script>