JSON数据格式错误

时间:2017-04-05 06:20:18

标签: javascript json

我正在尝试使用url将数据提取到var并使用if和else条件进行进一步操作如果条件是field3 == 1它应该执行如果stt即我们已经计算了两个时间戳之间的时间差当field3为1时使用created_at json数据值,否则使用stt如果field3 == 0。

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

    var json_data = {created_at:feed.created_at,field3:feed.field3};
    console.log(json_data);
    var feeds = [];
    for(var i in json_data)
    feeds.push(json_data [i]);
    console.log(feeds);
    var powercon = 746 * 0.5;
    console.log(powercon);
    power();
    function power(){
        for (var i=0;i<feeds.length;i++) //feeds should contain the JSON data like mentioned above
        {
            if(feeds[i]["field3"] === "1"){       
            //Start Time
            var d1 = new Date(feeds[i]["created_at"]);
            console.log(d1);
            for (var j=i+1;j<feeds.length;j++) //compare objects starting from 'i' till next "1" is found
            {   
                if(feeds[j]["field3"] === "0")  
                {
                    //End Time
                    var d2 = new Date(feeds[j]["created_at"]);
                    console.log(d2);
                    j=feeds.length;
                }   
            }
            //Time difference in milli seconds
            var diff = d2.getTime() - d1.getTime();
            var diffInSec = diff / 1000; //Convert milliseconds to seconds
            console.log(diffInSec);
            var diffInMin = diff/ 60 / 1000; // Convert milliseconds to minutes
            console.log(diffInMin);
            var diffInHours = diff/ 3600 / 1000; // Convert milliseconds to hours
            console.log(diffInHours);
            var pow = powercon * diffInHours;
            console.log(pow);
            }else{
                 console.log("Field3 is 0");
                 }
            }
        }

JsonData如下:

[{
        "created_at": "2017-03-14T01:00:32Z",
        "entry_id": 33358,
        "field1": "4",
        "field2": "4",
        "field3": "0"
    },
    {
        "created_at": "2017-03-14T01:00:42Z",
        "entry_id": 33357,
        "field1": "4",
        "field2": "4",
        "field3": "1"
    },
    {
        "created_at": "2017-03-14T01:00:50Z",
        "entry_id": 33356,
        "field1": "4",
        "field2": "4",
        "field3": "0"
    },
    {
        "created_at": "2017-03-14T02:44:08Z",
        "entry_id": 33359,
        "field1": "7",
        "field2": "2",
        "field3": "1"
    },
    {
        "created_at": "2017-03-14T02:45:51Z",
        "entry_id": 33360,
        "field1": "1110",
        "field2": "4",
        "field3": "0"
    },
    {
        "created_at": "2017-03-14T14:45:36Z",
        "entry_id": 33361,
        "field1": "17",
        "field2": "7",
        "field3": "0"
    },
    {
        "created_at": "2017-03-14T14:55:38Z",
        "entry_id": 33362,
        "field1": "17",
        "field2": "7",
        "field3": "0"
    },
    {
        "created_at": "2017-03-14T15:05:39Z",
        "entry_id": 33363,
        "field1": "17",
        "field2": "7",
        "field3": "0"
    },
    {
        "created_at": "2017-03-14T15:15:40Z",
        "entry_id": 33364,
        "field1": "17",
        "field2": "7",
        "field3": "0"
    },
    {
        "created_at": "2017-03-14T16:35:52Z",
        "entry_id": 33372,
        "field1": "18",
        "field2": "7",
        "field3": "1"
    },
    {
        "created_at": "2017-03-14T16:41:54Z",
        "entry_id": 33373,
        "field1": "14",
        "field2": "16",
        "field3": "1"
    },
    {
        "created_at": "2017-03-14T16:45:58Z",
        "entry_id": 33374,
        "field1": "9",
        "field2": "21",
        "field3": "1"
    }]

以上json数据我必须将它存储在var feed中并进行进一步处理。任何人都可以帮我解决这个问题吗??谢谢你。

1 个答案:

答案 0 :(得分:-1)

var obj = {a:1, b:2};

temp1 = JSON.stringify(obj);
console.log(temp1) //"{"a":1,"b":1}"

temp2 = JSON.parse(temp1);
console.log(temp2) //Object {a: 1, b: 2}
console.log(temp2.a) //1