使用json数据创建数组

时间:2014-03-18 06:11:01

标签: javascript json

我的JSON数据

[
    {"month":"May-2013","total_sales":"1369250"},
    {"month":"June-2013","total_sales":"4328119"},
    {"month":"July-2013","total_sales":"4636663"},
    {"month":"August-2013","total_sales":"4754047"},
    {"month":"September-2013","total_sales":"5014683"}
]

如何通过以下数据在javascript中创建数组?

2 个答案:

答案 0 :(得分:-1)

 var data = '[{"month":"May-2013","total_sales":"1369250"},
              {"month":"June-2013","total_sales":"4328119"},
              {"month":"July-2013","total_sales":"4636663"},
              {"month":"August-2013","total_sales":"4754047"},
              {"month":"September-2013","total_sales":"5014683"}]'

 var json = JSON.parse(data);
 console.log(json);

您基本上拥有 JSON

中的数组对象

同时检查http://www.mkyong.com/javascript/how-to-access-json-object-in-javascript/

并将其转换回json ..

var myJsonString = JSON.stringify(yourArray);

答案 1 :(得分:-1)

尝试这个

var json_data = [{"month":"May-2013","total_sales":"1369250"},{"month":"June-2013","total_sales":"4328119"},{"month":"July-2013","total_sales":"4636663"},{"month":"August-2013","total_sales":"4754047"},{"month":"September-2013","total_sales":"5014683"}];
var result = [];
for(var i in json_data)
   result.push([i, json_data [i]]);