json格式的国家数据到分层数据

时间:2015-04-01 11:48:16

标签: javascript json underscore.js

我有这个json数据data:[ {'country':'uk','district':'whitefield','ward':'eastward','county':'eastcounty1','company':'privatecompany'}, {'country':'uk','district':'whitefield','ward':'eastward','county':'eastcounty1','company':'privatecompany2'}, {'country':'uk','district':'whitefield','ward':'eastward','county':'eastcounty2','company':'privatecompany3'}, {'country':'uk','district':'whitefield','ward':'westward','county':'westcounty1','company':'privatecompany'}, {'country':'uk','district':'blackfield','ward':'westward','county':'eastcounty1','company':'privatecompany'}, {'country':'india','district':'andhra','ward':'ramnagar','county':'','company':'privatecompany'} ]

我必须将其转换为像......一样的层级结构。

data:{'uk':{
        "whitefield":{
          "eastward":{
            "eastcounty1":["privateCompany","privateCompany2"],
            "eastcounty2":["privatecompany3"]
            },
          "westward":{
            "westcounty1":["privatecompany"],
            "eastcounty1":["privatecompany"]
            }
          },
       "blackfield":{
           "westward":{"eastcounty1":["privatecompany"]}                
         }
     },
"india":{
   "andhra":{
      "ramnagar":["privatecompany"]
      }
  }}

在上面的例子中,我在一个场景中有county =“”或county = null,因此它被替换为ramnagar病房中的上面所示。

1 个答案:

答案 0 :(得分:1)

你只需要遍历每个项目并构建你的json对象Fiddle here

function mergeData(data) {
    var outp = {};

    // loop through each object and add
    for (var i = 0; i < data.length; i++) {
        var obj = data[i];
        if (outp.hasOwnProperty(obj.country)) {
            if (outp[obj.country].hasOwnProperty(obj.district)) {
                if (outp[obj.country][obj.district].hasOwnProperty(obj.ward)) {
                    if (outp[obj.country][obj.district][obj.ward].hasOwnProperty(obj.county)) {
                        outp[obj.country][obj.district][obj.ward][obj.county].push(obj.company);
                    }
                    else {
                        if (obj.county == "" || obj.county == null) {
                            if (Array.isArray(outp[obj.country][obj.district][obj.ward])) {
                                outp[obj.country][obj.district][obj.ward].push(obj.company);
                            }
                            else {
                                outp[obj.country][obj.district][obj.ward] = [obj.company];
                            }
                        }
                        else {
                            outp[obj.country][obj.district][obj.ward][obj.county] = [obj.company];
                        }
                    }
                }
                else {
                    outp[obj.country][obj.district][obj.ward] = {};
                    outp[obj.country][obj.district][obj.ward][obj.county] = [obj.company];                    
                }
            } else {
                outp[obj.country][obj.district] = {};
                outp[obj.country][obj.district][obj.ward] = {};
                outp[obj.country][obj.district][obj.ward][obj.county] = [obj.company];
            }
        } else {
            outp[obj.country] = {};
            outp[obj.country][obj.district] = {};
            
            if (obj.county == "") {
                outp[obj.country][obj.district][obj.ward] = [obj.company];
            }
            else {
                outp[obj.country][obj.district][obj.ward] = {};
                outp[obj.country][obj.district][obj.ward][obj.county] = [obj.company];
            }

        }
    }

    console.log(JSON.stringify(outp));
    return outp;
}

function doMerge() {

    var data = [{
        'country': 'uk',
        'district': 'whitefield',
        'ward': 'eastward',
        'county': 'eastcounty1',
        'company': 'privatecompany'
    }, {
        'country': 'uk',
        'district': 'whitefield',
        'ward': 'eastward',
        'county': 'eastcounty1',
        'company': 'privatecompany2'
    }, {
        'country': 'uk',
        'district': 'whitefield',
        'ward': 'eastward',
        'county': 'eastcounty2',
        'company': 'privatecompany3'
    }, {
        'country': 'uk',
        'district': 'whitefield',
        'ward': 'westward',
        'county': 'westcounty1',
        'company': 'privatecompany'
    }, {
        'country': 'uk',
        'district': 'blackfield',
        'ward': 'westward',
        'county': 'eastcounty1',
        'company': 'privatecompany'
    }, {
        'country': 'india',
        'district': 'andhra',
        'ward': 'ramnagar',
        'county': '',
        'company': 'privatecompany'
    }];

    mergeData(data);
}
<button onclick="doMerge()">Merge</button>