更改嵌套 JSON 的结构

Change the structure of nested JSON

本文关键字:结构 JSON 嵌套      更新时间:2023-09-26

我正在尝试更改 json 文件的结构。下面是用于当前结构的函数。我正在尝试更改当前函数,以便 json 的左右键将合并为子键。但是,我遇到了困难。你们能帮我修改代码或建议执行该功能的有效方法吗?

var buildTree = function(jsonObj){
      if(!jsonObj)
          return;
      for(var n in jsonObj){
          that.topicList.push(n);
          return{
                key : n,
                right : buildTree(jsonObj[n][0]),
                left : buildTree(jsonObj[n][1])
          }
      }
  }

此代码的输入:

{
"math": [{
    "Math": []
}, {
    "A Greek–English Lexicon": [{
        "A-list": []
    }, {
        "ASCII": []
    }]
}]
}

电流输出:

{
"key": "math",
"right": {
    "key": "Math"
},
"left": {
    "key": "A Greek–English Lexicon",
    "right": {
        "key": "A-list"
    },
    "left": {
        "key": "ASCII"
    }
}
}

我想将上面的输出更改为如下所示的输出:

{
"name": "math",
"child": [
  {
    "name": "Math",
    "children" :[]
},
{
    "name": "A Greek–English Lexicon",
    "child": [
      {
        "name": "A-list",
        "child" : []
        },
        {
        "name": "ASCII",
        "child" : []
        }
    ]
}
]}

这是一种递归方法,它返回一个新对象。

var object = { "math": [{ "Math": [] }, { "A Greek–English Lexicon": [{ "A-list": [] }, { "ASCII": [] }] }] },
    newObject = {};
function restyle(obj) {
    var k = Object.keys(obj)[0];
    return {
        key: k,
        child: obj[k].map(restyle)
    };
};
newObject = restyle(object);
document.write('<pre>' + JSON.stringify(newObject, 0, 4) + '</pre>');

这是一种递归方法,可以原位更改对象。

function restyle(o) {
    Object.keys(o).forEach(function (k) {
        o.key = k;
        o.child = o[k];
        delete o[k];
        o.child.forEach(restyle);
    });
};
var object = { "math": [{ "Math": [] }, { "A Greek–English Lexicon": [{ "A-list": [] }, { "ASCII": [] }] }] };
restyle(object);
document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');

我为此编写了一个解决方案。你基本上需要做递归编程。如果有错误,您可能需要进行一些基本的更改,但基本上我已经编写了逻辑和代码。它将递归解析子元素,直到找到一个空数组,即叶节点。我假设永远只有两个孩子,因为它看起来像一棵简单的树。

            /*Initially the object is pased here*/
            function parse(obj){
                /*Im assuming that the object has a key and value you need to add other failure checks here*/
                var keys =Object.keys(obj)
                return {
                    "name": keys[0]
                    "child" getChilds(obj[keys[0]])
                }
            }
            /*This is a recursive function which will grab left and right child and finally return the output.*/
            function getChilds(arr){

                    if(arr.length === 0){
                        return []
                    }
                    var obj = arr[0]
                    var keys =Object.keys(obj)

                    var newObj = {}
                    /*left child*/
                    var left = {
                        "name":keys[0],
                        "child":getChilds( obj[keys[0]] )
                    }
                    var obj = arr[1]
                    var keys =Object.keys(obj)
                    /*right child*/
                    var right = {
                        "name":keys[0],
                        "child":getChilds( obj[keys[0]] )
                    }
                    return [left,right]

            }