【问题标题】:How to convert a JSON dictionary to a list of values? [duplicate]如何将 JSON 字典转换为值列表? [复制]
【发布时间】:2016-10-11 00:40:56
【问题描述】:

我有以下 JSON 字符串:

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

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

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

我尝试了很多次,但都没有成功。请帮忙!

【问题讨论】:

  • 真的两次"label" : "Student"?

标签: javascript json


【解决方案1】:

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

【讨论】:

  • 非常感谢@Nina。现在它完成了。节省了我的时间。
  • ES2015 获胜:var array = Object.values(JSONstring);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 2013-02-14
  • 2021-02-03
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
相关资源
最近更新 更多