我有以下JSON字符串:
{"Local People":{"label":"Local People","data":1},"Student":{"label":"Student","data":1}}
我想将其转换为以下内容:
[{
"label" : "Student",
"data" : 1
},
{
"label" : "Student",
"data" : 1
}]
我已经尝试了很多次但是我没有成功。请帮忙!
答案 0 :(得分:3)
使用JSON.parse
解析字符串后,您可以获取对象的键并迭代属性以获取包含项目的新数组。
var JSONstring='{ "Local People": { "label": "Local People", "data": 1 }, "Student": { "label": "Student", "data": 1 } }'
object = JSON.parse(JSONstring),
array = Object.keys(object).map(function(k) {
return object[k];
});
console.log(array);