简体   繁体   English

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

[英]How to push JSON object in to array using javascript

I am trying to fetch the JSON data from an url.It is in the form of object i have to push the object data into array. 我试图从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"]
}); 

I have tried as above in my_json var i have the json data in the form of object now i have to store that object in to var data as an array in the below format 我已经在my_json中尝试了如上所述我有对象形式的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"
 }];

Can anyone help me how to do it??Thankyou 任何人都可以帮我怎么做??谢谢你

Observation 意见

  • If there is a single object and you want to push whole object into an array then no need to iterate the object. 如果存在单个对象并且您想将整个对象推入数组,则无需迭代该对象。

Try this : 尝试这个 :

 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); 

Instead of : 代替 :

 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); 

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);

can you try something like this. 你可以尝试这样的事吗? You have to put each json in the data not json[i], because in the way you are doing it you are getting and putting only the properties of each json. 你必须将每个json放在数据中而不是json [i],因为在你这样做的过程中,你只能得到每个json的属性。 Put the whole json instead in the data 把整个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);
    }); 

You need to have the 'data' array outside of the loop, otherwise it will get reset in every loop and also you can directly push the json. 你需要在循环之外使用'data'数组,否则它会在每个循环中重置,你也可以直接推送json。 Find the solution below:- 找到以下解决方案: -

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM