将JSON对象重新映射到其他JSON结构

时间:2017-01-16 11:59:38

标签: javascript json node.js

我正在尝试重新映射以类别格式化的以下JSON结构,然后每个类别可以包含多个位置。位置包含lon / lat和区号:

{
  "cat1":[
    {"location":{
      "latitude":51.38,
      "longitude":4.34,
      "code":"0873"}
    },
    {"location":{
      "latitude":52.65,
      "longitude":6.74,
      "code":"0109"}
    },
    {"location":{
      "latitude":51.48,
      "longitude":4.33,
      "code":"0748"}
    },
    {"location":{
      "latitude":51.48,
      "longitude":4.33,
      "code":"0109"}
    }
  ],
  "cat2":[
    {"location":{
      "latitude":52.33,
      "longitude":4.32,
      "code":"0873"}
    },
    {"location":{
      "latitude":52.65,
      "longitude":6.74,
      "code":"0109"}
    },
    {"location":{
      "latitude":51.48,
      "longitude":4.33,
      "code":"0728"}
    }
  ],
  "cat3":[
    {"location":{
      "latitude":52.33,
      "longitude":4.32,
      "code":"0873"}
    },
    {"location":{
      "latitude":52.65,
      "longitude":6.74,
      "code":"0109"}
    },
    {"location":{
      "latitude":51.48,
      "longitude":4.33,
      "code":"0758"}
    }
  ]
}

进入下面的结构,主要关注于areacode,然后是具有实际位置的类别。

{
  "code":[
    {"0873":[
      {"cat1":[
        {"location":{"latitude":51.38,"longitude":4.34}}
      ]},
      {"cat2":[
        {"location":{"latitude":52.33,"longitude":4.32}}
      ]},
      {"cat3":[
        {"location":{"latitude":52.33,"longitude":4.32}}
      ]}
    ]},

    {"0109":[
      {"cat1":[
        {"location":{"latitude":52.65,"longitude":6.74}},
        {"location":{"latitude":51.48,"longitude":4.33}}
      ]},
      {"cat2":[
        {"location":{"latitude":52.65,"longitude":6.74}}
      ]},
      {"cat3":[
        {"location":{"latitude":52.65,"longitude":6.74}}
      ]}

    ]},
    {"0748":[
      {"cat1":[
        {"location":{"latitude":51.48,"longitude":4.33}}
      ]}
    ]},

    {"0728":[
      {"cat2":[
        {"location":{"latitude":51.48,"longitude":4.33}}
      ]}
    ]},
    {"0758":[
      {"cat3":[
        {"location":{"latitude":51.48,"longitude":4.33}}
      ]}
    ]}

  ]
}

我尝试在Javascript / Node中执行此操作,并且正在寻找一种比手动遍历所有对象并重构它们更优雅的方法。 正在调查reorientobstruction,但无法找到完成任务的方法....

感谢任何帮助!

我知道上面的部分是从文件中读取然后解析为对象的JSON字符串。

我必须执行此操作的代码(尚未完成,因为我不知道执行remapJson()的最佳方法是什么:

var fs = require('fs'),
jsonfile = require('jsonfile');

function remapJson(oldData) {
  var newData = {};

  // Do the convertion (loop all keys and values?)


  return newData
}

obj = jsonfile.readFileSync('oldstructure.json');

jsonfile.writeFileSync('newstructure.json', remapJson(obj));

2 个答案:

答案 0 :(得分:3)

您可以使用迭代和递归方法在哈希表中寻址结果数组。



var data = { cat1: [{ location: { latitude: 51.38, longitude: 4.34, code: "0873" } }, { location: { latitude: 52.65, longitude: 6.74, code: "0109" } }, { location: { latitude: 51.48, longitude: 4.33, code: "0748" } }, { location: { latitude: 51.48, longitude: 4.33, code: "0109" } }], cat2: [{ location: { latitude: 52.33, longitude: 4.32, code: "0873" } }, { location: { latitude: 52.65, longitude: 6.74, code: "0109" } }, { location: { latitude: 51.48, longitude: 4.33, code: "0728" } }], cat3: [{ location: { latitude: 52.33, longitude: 4.32, code: "0873" } }, { location: { latitude: 52.65, longitude: 6.74, code: "0109" } }, { location: { latitude: 51.48, longitude: 4.33, code: "0758" } }] },
    result = { code: [] };

Object.keys(data).forEach(function (key) {
    data[key].forEach(function (a) {
        [a.location.code, key].reduce(function (r, k) {
            var o = {};
            if (!r[k]) {
                r[k] = { _: [] };
                o[k] = r[k]._;
                r._.push(o);
            }
            return r[k];
        }, this)._.push({ location: { latitude: a.location.latitude, longitude: a.location.longitude } });
    }, this);
}, { _: result.code });

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:0)

试试这个

var a = //your json
var newArray = [];
for(var i in a){
    var obj = {};
    var newobj = {};
    var innerobj = {};
    var locationobj = {};
    if(a[i].length != 0){
        var keyname = i;
        for(var j = 0;j<a[i].length ; j++){
            if(a[i][j].location.code == "0873"){
                locationobj["latitude"] = a[i][j].location.latitude;
                locationobj["longitude"] = a[i][j].location.longitude;
                innerobj[i] = {"loaction":locationobj};
                obj[a[i][j].location.code] = innerobj;
                newArray.push({"code":obj})
            }else if(a[i][j].location.code == "0758"){
                locationobj["latitude"] = a[i][j].location.latitude;
                locationobj["longitude"] = a[i][j].location.longitude;
                innerobj[i] = {"loaction":locationobj};
                obj[a[i][j].location.code] = innerobj;
                newArray.push({"code":obj})
            }else if(a[i][j].location.code == "0109"){
                locationobj["latitude"] = a[i][j].location.latitude;
                locationobj["longitude"] = a[i][j].location.longitude;
                innerobj[i] = {"loaction":locationobj};
                obj[a[i][j].location.code] = innerobj;
                newArray.push({"code":obj})
            }

        }
    }
}

console.log(newArray)