当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Create Array using json data

问题描述:

My JSON data are

[

{"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"}

]

How can i craete an array in javascript from following data?

网友答案:

try this one

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]]);
网友答案:
 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);

You basically have the array object in JSON

Check http://www.mkyong.com/javascript/how-to-access-json-object-in-javascript/ also.

And to convert it back to json..

var myJsonString = JSON.stringify(yourArray);
分享给朋友:
您可能感兴趣的文章:
随机阅读: