如何将JSON字典转换为值列表?

时间:2016-06-10 06:43:27

标签: javascript json

我有以下JSON字符串:

{"Local People":{"label":"Local People","data":1},"Student":{"label":"Student","data":1}}

我想将其转换为以下内容:

[{
    "label" : "Student",
    "data" : 1
},
{
    "label" : "Student",
    "data" : 1
}]

我已经尝试了很多次但是我没有成功。请帮忙!

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);