重复重命名json键

时间:2016-11-25 16:27:06

标签: javascript json

我有一个非常简单的json,但在每个区块中我得到了类似的东西。

var json = {
"name": "blabla"
"Children": [{
      "name": "something"
      "Children": [{ ..... }]
}

等等。我不知道每个孩子里面有多少孩子递归。

var keys = Object.keys(json);

for (var j = 0; j < keys.length; j++) {
    var key = keys[j];
    var value = json[key];
    delete json[key];
    key = key.replace("Children", "children");
    json[key] = value;
}

现在我想替换所有&#34;孩子&#34;小写的键#34;孩子&#34;。以下代码仅适用于第一个深度。我怎么能递归地做到这一点?

4 个答案:

答案 0 :(得分:3)

看起来输入结构非常明确,所以你可以简单地创建一个这样的递归函数:

function transform(node) {
  return {
    name: node.name,
    children: node.Children.map(transform)
  };
}


var json = {
  "name": "a",
  "Children": [{
    "name": "b",
    "Children": [{
      "name": "c",
      "Children": []
    }, {
      "name": "d",
      "Children": []
    }]
  }, {
    "name": "e",
    "Children": []
  }]
};

console.log(transform(json));

答案 1 :(得分:2)

可能的解决方案:

var s = JSON.stringify(json);
var t = s.replace(/"Children"/g, '"children"');
var newJson = JSON.parse(t);
  • 优点:此解决方案非常简单,仅为三行。
  • 缺点: 潜在的不必要的副作用,请考虑:

    var json = {
      "name": "blabla",
      "Children": [{
        "name": "something",
        "Children": [{ ..... }]
      }],
      "favouriteWords": ["Children","Pets","Cakes"]
    }
    

该解决方案取代了 "Children"的所有实例,因此favouriteWords数组中的条目也会被替换,尽管属性名称。如果除了作为属性名称之外没有其他任何地方出现的话,那么这不是问题,但值得提出以防万一。

答案 2 :(得分:1)

这是一个可以递归执行的函数:

 function convertKey(obj) {
    for (objKey in obj)
    {

        if (Array.isArray(obj[objKey])) {
            convertKey[objKey].forEach(x => {
                convertKey(x);
            });

        }

        if (objKey === "Children") {
            obj.children = obj.Children;
            delete obj.Children;
        }
    }
 }

这是一种更通用的方法:

function convertKey(obj, oldKey, newKey) {
    for (objKey in obj)
    {

        if (Array.isArray(obj[objKey])) {
            obj[objKey].forEach(objInArr => {
                convertKey(objInArr);
            });
        }

        if (objKey === oldKey) {
            obj[newKey] = obj[oldKey];
            delete obj[oldKey];
        }
    }
}

convertKey(json, "Children", "children");

答案 3 :(得分:0)

接受的答案和@Tamas答案都有轻微的问题。

正如@Bardy的答案所指出的那样,如果你的任何一个价值观中有“儿童”这个词会导致问题,那就有问题。

对于@Tamas,一个问题是除名称和其他属性之外的任何其他属性。孩子们掉线了。它还假设儿童财产。如果子属性已经children而不是Children,该怎么办?

使用稍微修改过的@Tamas版本,这可以避免陷阱。

&#13;
&#13;
function transform(node) {
  if (node.Children) node.children = node.Children;
  if (node.children) node.children = node.children.map(transform);
  delete node.Children;
  return node;
}


var json = {
  "name": "a",
  "Children": [{
    "age": 13,
    "name": "b",
    "Children": [{
      "name": "Mr Bob Chilren",
      "Children": []
    }, {
      "name": "d",
      "age": 33,  //other props keep
      "children": [{
        "name": "already lowecased",
        "age": 44,
        "Children": [{
           "name": "now back to upercased",
           "age": 99
        }]
      }] //what if were alrady lowercased?
    }]
  }, {
    "name": "e",
    //"Children": []  //what if we have no children
  }]
};

console.log(transform(json));
&#13;
&#13;
&#13;