在嵌套的JSON结构中更改密钥名称 [英] Change key name in nested JSON structure
本文介绍了在嵌套的JSON结构中更改密钥名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个JSON数据结构,如下所示:
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?
推荐答案
我不知道为什么你有一个您的JSON标记结尾处的分号(假设您在问题中表示的内容),但如果删除了分号,则可以使用 reviver函数进行修改解析数据时。
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/
这篇关于在嵌套的JSON结构中更改密钥名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文