最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change key name in nested JSON structure - Stack Overflow

programmeradmin2浏览0评论

I have a JSON data structure as shown below:

{
    "name": "World",
    "children": [
      { "name": "US",
          "children": [
           { "name": "CA" },
           { "name": "NJ" }
         ]
      },
      { "name": "INDIA",
          "children": [
          { "name": "OR" },
          { "name": "TN" },
          { "name": "AP" }
         ]
      }
 ]
};

I need to change the key names from "name" & "children" to say "key" & "value". Any suggestion on how to do that for each key name in this nested structure?

I have a JSON data structure as shown below:

{
    "name": "World",
    "children": [
      { "name": "US",
          "children": [
           { "name": "CA" },
           { "name": "NJ" }
         ]
      },
      { "name": "INDIA",
          "children": [
          { "name": "OR" },
          { "name": "TN" },
          { "name": "AP" }
         ]
      }
 ]
};

I need to change the key names from "name" & "children" to say "key" & "value". Any suggestion on how to do that for each key name in this nested structure?

Share Improve this question edited Nov 22, 2012 at 19:13 John Kugelman 362k69 gold badges548 silver badges594 bronze badges asked Nov 22, 2012 at 19:08 user1842231user1842231 2131 gold badge3 silver badges8 bronze badges 2
  • When/where do you want to make the change? – I Hate Lazy Commented Nov 22, 2012 at 19:19
  • ...and why do you have a semicolon at the end? What exactly are you representing in the question? – I Hate Lazy Commented Nov 22, 2012 at 19:33
Add a comment  | 

3 Answers 3

Reset to default 17

I don't know why you have a semicolon at the end of your JSON markup (assuming that's what you've represented in the question), but if that's removed, then you can use a reviver function to make modifications while parsing the data.

var parsed = JSON.parse(myJSONData, function(k, v) {
    if (k === "name") 
        this.key = v;
    else if (k === "children")
        this.value = v;
    else
        return v;
});

DEMO: http://jsfiddle.net/BeSad/

Try this:

function convert(data){
  return {
    key: data.name,
    value: data.children.map(convert);
  };
}

Or if you need to support older browsers without map:

function convert(data){
  var children = [];
  for (var i = 0, len = data.children.length; i < len; i++){
    children.push(convert(data.children[i]));
  }

  return {
    key: data.name,
    value: children
  };
}

You could use a function like this :

function clonerename(source) {
    if (Object.prototype.toString.call(source) === '[object Array]') {
        var clone = [];
        for (var i=0; i<source.length; i++) {
            clone[i] = goclone(source[i]);
        }
        return clone;
    } else if (typeof(source)=="object") {
        var clone = {};
        for (var prop in source) {
            if (source.hasOwnProperty(prop)) {
                var newPropName = prop;
                if (prop=='name') newPropName='key';
                else if (prop=='children') newPropName='value';
                clone[newPropName] = clonerename(source[prop]);
            }
        }
        return clone;
    } else {
        return source;
    }
}

var B = clonerename(A);

Note that what you have isn't a JSON data structure (this doesn't exist as JSON is a data-exchange format) but probably an object you got from a JSON string.

发布评论

评论列表(0)

  1. 暂无评论