简体   繁体   English

更改嵌套JSON的结构

[英]Change the structure of nested JSON

I m trying to change the structure of the json file. 我正在尝试更改json文件的结构。 Below is the function that is being used for the current structure. 以下是当前结构所使用的功能。 I m trying to change the current function so the right and left key of the json will be merged as child. 我正在尝试更改当前功能,因此json的左右键将合并为子级。 However, I m facing difficulty with it. 但是,我面临着困难。 Can you guys help me to modify the code or suggest an efficient way to perform the function? 你们可以帮我修改代码或建议执行该功能的有效方法吗?

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])
          }
      }
  }

The input for this code: 此代码的输入:

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

Current output: 电流输出:

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

I want to change the above output into the one like below: 我想将上面的输出更改为如下所示:

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

This is a recursive approach, which returns a new object. 这是一种递归方法,它返回一个新对象。

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

This is a recursive approach, which changes the object 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>'); 

I wrote a solution for this. 我为此写了一个解决方案。 You basically need to do recursive programming. 您基本上需要进行递归编程。 You may need to do some basic changes if there are errors but essentially i've written the logic and the code. 如果有错误,您可能需要做一些基本的更改,但实际上我已经编写了逻辑和代码。 It will recursively parse through the child elements until it finds an empty array, ie the leaf node. 它将递归地解析子元素,直到找到一个空数组,即叶节点。 Im assuming there will always be only two child since it looked like a simple tree. 我假设永远只有两个孩子,因为它看起来像一棵简单的树。

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


            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM