【问题标题】:Convert a json object data to a json array data?将 json 对象数据转换为 json 数组数据?
【发布时间】:2014-01-02 19:13:49
【问题描述】:

如何将 json 对象数据转换为 json 数组数据?例如,

我在 firebug 的控制台上有来自服务器的这个 json 对象数据,

Object { 0={...}, 1={...}, 2={...}, more...}

json 字符串,

{
    "items": {
        "0": {
            "pub_name": "News",
            "system_id": "57",
            "make_accessible": "0",
            "count": "7",
            "router": "#/post/news/list/"
        },
        "1": {
            "pub_name": "upcoming events",
            "system_id": "134",
            "make_accessible": "0",
            "count": "7",
            "router": null
        }
    },
    "total": 2
}

我需要将其转换为,

[Object { pub_name="Main", system_id="50", make_accessible="0", more...}, Object { pub_name="estate", system_id="122", make_accessible="0", more...}, Object { pub_name="wines", system_id="125", make_accessible="0", more...}, 7 more...]

json 字符串,

{
    "items": [
        {
            "pub_name": "News",
            "system_id": "57",
            "make_accessible": "0",
            "count": "7",
            "router": "#/post/news/list/"
        },
        {
            "pub_name": "upcoming events",
            "system_id": "134",
            "make_accessible": "0",
            "count": "7",
            "router": null
        }
    ],
    "total": 2
}

有可能吗?

【问题讨论】:

  • 是的,有可能。但是请注意,json 与此无关。您需要遍历对象中的键,创建所需的数组。
  • 您将如何决定对象的元素名称?你从哪里得到“pub_name”和“system_id”?
  • 只是因为这有点让我烦恼,所以没有“json 对象”或“json 数组”之类的东西。这些条款是不正确的。 JSON 是一种数据传输格式(类似于 CSV 或 XML)。 JSON 总是 一个字符串。它可以表示一个对象或一个数组,但它本身始终是一个字符串。如果不是字符串,则不是 JSON。您拥有的是一个 JavaScript 对象和一个 JavaScript 数组。它可能是从 JSON 字符串解析,但不是 JSON。
  • 如果有意义,请查看我上面的编辑?
  • @lauthiamkok:首先你是如何创建这个 JSON 字符串的?您也许可以将其以正确的格式退还给您。

标签: jquery arrays json object


【解决方案1】:

解决此问题的最佳方法是修复它在服务器端的生成方式,可能通过将 Items 结构转换为您使用的任何服务器端语言的数组。

但是,如果 javascript 是您唯一的选择,您需要做的就是遍历对象键,将它们分配给一个数组。

var jsonstring = '{\
    "items": {\
        "0": {\
            "pub_name": "News",\
            "system_id": "57",\
            "make_accessible": "0",\
            "count": "7",\
            "router": "#/post/news/list/"\
        },\
        "1": {\
            "pub_name": "upcoming events",\
            "system_id": "134",\
            "make_accessible": "0",\
            "count": "7",\
            "router": null\
        }\
    },\
    "total": 2\
}';

var data = JSON.parse(jsonstring);
console.log(data); // {items: Object, total: 2}

var tempArr = [];
for (key in data.items) {
    tempArr[key] = data.items[key];
}
data.items = tempArr;
console.log(data); // {items: Array[2], total: 2}

http://jsfiddle.net/nje2e/

如果你有 jQuery,你可以使用$.makeArray();

var data = JSON.parse(jsonstring);
console.log(data); // {items: Object, total: 2}
data.items.length = data.total; // adding a length makes it array-like
data.items = $.makeArray(data.items);
console.log(data); // {items: Array[2], total: 2}

http://jsfiddle.net/nje2e/1/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2020-10-02
    • 1970-01-01
    • 2019-12-07
    • 2016-01-28
    相关资源
    最近更新 更多