将JSON对象重新映射到其他JSON结构(Remap JSON object to other JSON structure)

编程入门 行业动态 更新时间:2024-10-10 07:20:53
将JSON对象重新映射到其他JSON结构(Remap JSON object to other JSON structure)

我正在尝试重新映射以类别格式化的以下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中执行此操作,并且正在寻找一种比手动遍历所有对象并重构它们更优雅的方法。 正在调查重新定位和阻碍,但无法找到完成它的方法....

任何帮助表示赞赏!

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

目前我必须执行的代码( remapJson()没有完成,因为我不知道做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));

I'm trying to remap the following JSON structure that is formatted by categories and then per category can contain multiple locations. A location contains a lon/lat and an area code:

{
  "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"}
    }
  ]
}
 

Into the following structure, that is primarily focussed on the areacode, then has the categories with the actual locations in them.

{
  "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}}
      ]}
    ]}

  ]
}
 

I try to do this in Javascript/Node and was looking at a way to do this more elegant than traversing all objects by hand and restructure them. Was looking into reorient and obstruction but can't find a way to get it done....

Any help is appreciated!

I know the pieces above are JSON strings that are read from file and then parsed to an object.

The code I have to do this is currently(its not finished, because I didn't know what would be the best way to do the 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));

                

最满意答案

您可以使用迭代和递归方法来处理哈希表中的结果数组。

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

You could use an iterative and recursive approach for addressing the result arrays in a hash table.

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

更多推荐

本文发布于:2023-07-30 15:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   结构   JSON   structure   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!