更改嵌套JSON的结构

时间:2016-03-02 12:23:27

标签: javascript json

我试图更改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" : []
        }
    ]
}
]}

2 个答案:

答案 0 :(得分:1)

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

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>');

这是一种递归方法,可以更改对象 in situ

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>');

答案 1 :(得分:0)

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

            /*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]


            }
相关问题