[英]Treating JSON as a tree structure
I have some incoming JSON which consists of only nested maps (no arrays), eg, 我有一些传入的JSON,它仅由嵌套地图(无数组)组成,例如,
{
"name1": {
"name2": "name3",
"name4": {
"name5": "name6"
}
},
"name7": "name8"
}
Notice that the keys and values here don't follow any particular pattern. 请注意,此处的键和值不遵循任何特定模式。
What I want is, given such an object representing the root of a tree, to produce a list of new objects representing the first layer of children, each as a new JSON object. 我想要的是,给定一个这样的对象,它表示树的根,以生成代表新的子对象的第一层的对象列表,每个对象都是一个新的JSON对象。
function ent(map) {
var entries = [];
for (var key in map)
entries.push({
key: key,
value: map[key]
});
return entries;
}
ent({
"name1": {"name2": "name3", "name4": {"name5": "name6"}},
"name7": "name8"
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.