javascript - 如何从 JsonObj 获取键值

标签 javascript jquery json

我有一个像这样的 json 对象,实际上它是动态生成的。 例如,我们可以使用这个 Json 对象。

var JsonObj= { "one":1, "two":2, "three":3, "four":4, "five":5 };

但我想使用 javascriptjquery 来改变它。

var sample = [

 {
  "label":"one",
  "value":1
 },

 {
  "label":"two",
  "value":2
 },
 {
  "label":"three",
  "value":3
 },
 { 
  "label":"four",
  "value":4
 },
 { 
  "label":"five",
  "value":5
  }

 ];

最佳答案

var JsonObj= { "one":1, "two":2, "three":3, "four":4, "five":5 };
var arr = [];
for(property in JsonObj) {
    arr.push(
             {"label":property, 
             "value":JsonObj[property]})
};

关于javascript - 如何从 JsonObj 获取键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31075804/

相关文章:

jQuery:removeClass 中的通配符类选择器

Android 连接到远程 mysql 数据库 JDBC 与 JSON

javascript - 是否可以在 Visual Studio 之外的不同 IDE 中开发 Win8 应用程序?

javascript - ext.log函数为空函数

javascript - 具有不同样式的多个 jQuery UI 日期选择器

javascript - 单击按钮执行 php 文件然后重定向

jquery - 确认 jQuery 可排序接收事件

javascript - 调试 ServiceStack POST 请求反序列化

c# - 为什么我的 C# json 反序列化失败?

javascript - 如何防止公众查看您的 GA 属性 ID