将数据编码为父/子json数据格式

时间:2013-08-01 09:35:33

标签: javascript

我有这样的json数据。

 var data=    [
{"categoryName":"VPN", "INCIDENT_ID":"IN006336", "DESCRIPTION":"vpn not complaint",    
"maximumHit":"2"},


{"categoryName":"VPN", "INCIDENT_ID":"IN006112", "DESCRIPTION":"VPN issues asked to   
reinstall vpn client", "maximumHit":"2"}, 


{"categoryName":"VPN", "INCIDENT_ID":"IN011902", "DESCRIPTION":"vpn installation link- 
itldrive", "maximumHit":"2"}];

并希望采用以下格式的数据

 var json2 =
   {
         "name": "Network Issue",
         "children": [
          {
           "name": "DESCRIPTION", 
           "children": 

            [
              {"name": "vpn not complaint"},
              {"name": "VPN issues asked to reinstall vpn client"},
              {"name": "vpn installation link- itldrive"}
             ]
          },

          {
           "name": "INCIDENT_ID",
           "children": [

            {"name": "IN006336"},
            {"name": "IN006112"},
                {"name": "IN006112"}
           ]
          },

          {
           "name": "categoryName",
           "children": [
               {"name": "VPN"},
            {"name": "VPN"},
                {"name": "VPN"}
           ]
          }
         ]
        }

1 个答案:

答案 0 :(得分:0)

也许是这样的?

var str = JSON.stringify(obj, undefined, 2);