在嵌套json中更改密钥名称的最快方法?

时间:2018-02-21 18:16:35

标签: javascript json

我的json看起来像这样

const myJson = [
  {

  "title":"1. Lockdown Population by Loan (186006)",
  children:[
     {
        title:"1.1 Previous (191225)",
        "firstLevelSubNodes:[
              { 
                title:"1.1.1 Roll forward (178260)" ,
                "firstLevelSubNodes":[{ 
                  title:"1.1.1.1 Roll forward (178260)"
                  }]
              }
        ],
        "secondLevelSubNodes":[
             title:"1.2.1 Bounce forward (178260)" ,
                "firstLevelSubNodes":[{ 
                  title:"1.2.1.1 Dash forward (178260)",
                    "firstLevelSubNodes":[{ 
                     title:"1.2.1.1.1 Skip forward (178260)",
                  }]
                  }]
        ],
        "thirdLevelSubNodes":[
        ],
        "fourthLevelSubNodes":[

        ]
     }         
  ],
  }
]

在任何地方,你都会看到xxxLevelSubNodes这个词,它需要像下面这样说出“儿童”这个词。

 const myJson = [
     {

  "title":"1. Lockdown Population by Loan (186006)",
  children:[
     {
        title:"1.1 Previous (191225)",
        "children:[
              { 
                title:"1.1.1 Roll forward (178260)" ,
                "children":[{ 
                  title:"1.1.1.1 Roll forward (178260)"
                  }]
              }
        ],
        "children":[
             title:"1.2.1 Bounce forward (178260)" ,
                "children":[{ 
                  title:"1.2.1.1 Dash forward (178260)",
                    "children":[{ 
                     title:"1.2.1.1.1 Skip forward (178260)",
                  }]
                  }]
        ],
        "children":[
        ],
        "children":[
        ]
     }         
  ],
  }
]

我试图循环但是它会将这个词重命名为子,但它会覆盖最后一个键。它也不会循环遍历JSON的嵌套部分。我需要它保持与上面完全相同的结构

const res = { firstLevelSubNodes: 'children', secondLevelSubNodes: 'children',

  thirdLevelSubNodes: 'children', fourthLevelSubNodes: 'children' }; 

 const object = JSON.parse(myJson);
    for (const k in res) {

      const newValue = res[k];

      object[newValue] = object[k];

      object[newValue].name = newValue;

      delete object[k];

    }

1 个答案:

答案 0 :(得分:3)

就像评论所说的人一样,只要因为JSON的工作方式,你所要求的就不可能了。

你需要重新思考你想要做的事情...... “这代表我们应用程序中的树视图。我们使用的自定义角度指令找到单词”children“并自动将其标识为子节点”

根据这个逻辑,每个级别应该只有1个子属性,这意味着你的json应该是这样的:

let myJson = [
    {

        "title":"1. Lockdown Population by Loan (186006)",
        children:[
            {
                title:"1.1 Previous (191225)",
                "children":[
                   {
                        title:"1.1.1 Roll forward (178260)" ,
                        "children":[{
                            title:"1.1.1.1 Roll forward (178260)"
                        }]
                    },
                    {
                        title: "1.2.1 Bounce forward (178260)",
                        "children": [{
                            title: "1.2.1.1 Dash forward (178260)",
                            "children": [{
                                title: "1.2.1.1.1 Skip forward (178260)",
                            }]
                        }]
                    },
                ],
            }
        ],
    }
];

如果结构是真的,那么你真正想做的是将所有属性与包含你格式的键合并到同一个数组中。

当然你想要递归地做这件事。

所以类似

function refactorJson(array) {
    for (var i =0; i< array.length; i++) {
        var obj = array[i];
        var newObj = {};
        newObj.title = obj.title;
        newObj.children = [];

        for (var property in obj) {
        console.log(property)
            if (property.contains('LevelSubNodes') || property === 'children') {
                var subObj = obj[property];
                var child = refactorJson(subObj);
                if (child) {
                    newObj.children.push(child);
                }
            }

        }
    }
    return newObj;
}





var json =  [{
    "title": "1. Lockdown Population by Loan (186006)",
    "children": [{
        "title": "1.1 Previous (191225)",
        "firstLevelSubNodes": [{
            "title": "1.1.1 Roll forward (178260)",
            "firstLevelSubNodes": [{
                "title": "1.1.1.1 Roll forward (178260)"
            }]
        }],
        "secondLevelSubNodes": [{
            "title": "1.2.1 Bounce forward (178260)",
            "firstLevelSubNodes": [{
                "title": "1.2.1.1 Dash forward (178260)",
                "firstLevelSubNodes": [{
                    "title": "1.2.1.1.1 Skip forward (178260)"
                }]
            }]
        }],
        "thirdLevelSubNodes": [],
        "fourthLevelSubNodes": [

        ]
    }]
}];

var newJson = formatJson(json)