我正在尝试从url获取JSON数据。它是以对象的形式,我必须将对象数据推送到数组中。
var my_json; $.getJSON("https://api.thingspeak.com/channels/"+did+"/feeds.json?api_key="+apikey+"&results=300", function(json1) { console.log(json1); json1.feeds.forEach(function(feed,i){ console.log("\n The details of " + i + "th Object are : \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2+"\nField3:" + feed.field3); my_json = feed; console.log(my_json); //Object {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"} var data = []; for(var i in my_json) data.push(my_json [i]); console.log(data); //["2017-03-14T01:00:32Z", 33358, "4", "4", "0"] });
我如上所述在my_json var中尝试过,我以对象形式存在json数据,现在我必须将该对象存储为var数据作为以下格式的数组
[{ "created_at": "2017-03-14T01:00:32Z", "entry_id": 33358, "field1": "4", "field2": "4", "field3": "0" }, { "created_at": "2017-03-14T22:54:43Z", "entry_id": 33398, "field1": "84", "field2": "32", "field3": "0" }];
谁能帮我怎么做?谢谢
观察
试试这个 :
var feed = {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"}; var data = []; data.push(feed); console.log(data);
代替 :
var my_json = {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"}; var data = []; for(var i in my_json) { data.push(my_json[i]); } console.log(data);