如何使用javascript将JSON对象推入数组

时间:2017-04-12 06:26:01

标签: javascript json

我正在尝试从url获取JSON数据。它是对象的形式我必须将对象数据推送到数组中。

newer version

我在my_json var中尝试了如上所述我有对象形式的json数据现在我必须将该对象作为数组存储到var数据中,格式如下

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"]
}); 

任何人都可以帮我怎么做吗?谢谢你

4 个答案:

答案 0 :(得分:14)

<强>观察

  • 如果只有一个对象,并且您想将整个对象推送到数组中,则无需迭代该对象。

试试这个:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

代替:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

var postdata = {created_at: "2017-03-14T01:00:32Z", entry_id: 33358, field1: "4", field2: "4", field3: "0"};

var data = [];
data.push(postdata);

console.log(data);

答案 2 :(得分:0)

你能尝试这样的事吗?你必须将每个json放在数据中而不是json [i],因为在你这样做的过程中,你只能得到每个json的属性。把整个json放在数据

 var my_json;
    $.getJSON("https://api.thingspeak.com/channels/"+did+"/feeds.json?api_key="+apikey+"&results=300", function(json1) {
    console.log(json1);
        var data = [];
    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"}
        data.push(my_json);
    }); 

答案 3 :(得分:0)

您需要拥有&#39;数据&#39;循环外的数组,否则它将在每个循环中重置,你也可以直接推送json。找到以下解决方案: -

var my_json;
$.getJSON("https://api.thingspeak.com/channels/"+did+"/feeds.json?api_key="+apikey+"&results=300", function(json1) {
console.log(json1);
var data = [];
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"}
    data.push(my_json);
     //["2017-03-14T01:00:32Z", 33358, "4", "4", "0"]
}); 
console.log(data);