2017-02-07 53 views
0

我想使用url.Jun取得json數據,現在我需要解析Json數據,以便只從url提取數據。我在下面添加代碼和json data.Can任何人都可以幫助我如何解析來自以下json output.thankyou的提要數據。使用https url解析Javascript JSON

JSON數據:

{ 
    "channel": { 
    "id": 9, 
    "name": "my_house", 
    "description": "Netduino Plus connected to sensors around the house", 
    "latitude": "40.44", 
    "longitude": "-79.996", 
    "field1": "Light", 
    "field2": "Outside Temperature", 
    "created_at": "2010-12-13T20:20:06-05:00", 
    "updated_at": "2014-02-26T12:43:04-05:00", 
    "last_entry_id": 6060625 
    }, 
    "feeds": [{ 
    "created_at": "2014-02-26T12:42:49-05:00", 
    "entry_id": 6060624, 
    "field1": "188", 
    "field2": "25.902335456475583" 
    }, { 
    "created_at": "2014-02-26T12:43:04-05:00", 
    "entry_id": 6060625, 
    "field1": "164", 
    "field2": "25.222929936305732" 
    }] 
} 

$.ajax({ 
    url: " https://api.thingspeak.com/channels/9/feeds.json?results=2", 
    dataType: "json", 
    cache: false, 
    error: function(xhr, ajaxOptions, thrownError) { 
    debugger; 
    alert(xhr.statusText); 
    alert(thrownError); 
    }, 
    success: function(json1) { 
    console.log(json1); 
    if (json1.length == 0) { 
     window.alert("The returned output array length is ZERO."); 
    } else { 
     var obj1, Feed; 
     for (var x = 0; x < json1.length; x++) { 
     obj1 = json1[x]; 
     console.log(obj1); 
     if (obj1 == null || obj1 == "") { 
      window.alert("\n The " + (x + 1) + "th object is NULL/BLANK."); 
     } else { 

      if (obj1.feeds == null || obj1.feeds.length == 0) { 
      window.alert("\n The name portion of " + (x + 1) + "th object is NULL/BLANK."); 
      } else { 
      Feed = obj1.feeds; 

      for (var k = 0; k < Feed.length; k++) { 
       console.log("\n The deails of " + (x + 1) + "th Object are : \nCreated_at: " + Feed[k].created_at + "\nEntry_id:" + Feed[k].entry_id + "\nField1:" + Feed[k].field1 + "\nField2:" + Feed[k].field2); 
      } 
      } 
     } 
     } 
    } 
    } 
}); 
+0

首先,這個長度有一個錯字在它:'如果(obj1.feeds == NULL || obj1.feeds.lenght == 0){' –

+0

推薦本Link.you會得到一些想法。 http://stackoverflow.com/questions/29936971/find-a-value-inside-array-of-json-object –

+0

你能告訴我那個錯字是什麼,我無法破解它@ Marc-AntoineParent – Anusha

回答

1

簡單解析個別飼料飼料數組:

json1.feeds.forEach(function(feed){ 
    console.log("th Object are : \nCreated_at: " + feed.created_at 
    + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2);  
}); 

https://plnkr.co/edit/WSgdO6dZsyIAQVxWkTzr?p=preview

+0

tq.It解決了我的問題@inQstvJs – Anusha

0

這裏的問題是,你嘗試的工作與str並嘗試獲取長度(json1.length),但jquery下爲您返回JSON對象(因爲您已指定dataType: "json")。
所以你必須不用字符串而是用對象工作!在你的例子中,你有簡單的對象,屬性爲feeds,因此你只需要遍歷json1.feeds中的數組。

這裏我的例子(我剛剛更新success callback):

json1.feeds.forEach(function(feed, i) { 
    // This is your code, I've just copy-paster it here. 
    console.log("\n The deails of " + i + "th Object are : \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2); 
}); 
+0

tq.It解決了我的問題@ Vladimir KovPak – Anusha

2

json1不是一個數組,它是一個對象。您需要訪問該對象的.feeds屬性以獲取提要數組。事情是這樣的:

$.ajax({ 
    url: " https://api.thingspeak.com/channels/9/feeds.json?results=2", 
    dataType: "json", 
    cache: false, 
    error: function(xhr, ajaxOptions, thrownError) { 
    debugger; 
    alert(xhr.statusText); 
    alert(thrownError); 
    }, 
    success: function(json) { 
    console.log(json); 
    if (!json || json.feeds === undefined || json.feeds.length === 0) { 
     window.alert("The returned output array length is ZERO."); 
    } else { 
     json.feeds.forEach(function (feed, index) { 
     var indexPlusOne = index + 1; 
     console.log("The deails of " + indexPlusOne + "th Object are : \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2); 
     }); 
    } 
    } 
}); 

你可以把它一點點更具可讀性,如果你使用ES2015和模板字符串:

$.ajax({ 
    url: " https://api.thingspeak.com/channels/9/feeds.json?results=2", 
    dataType: "json", 
    cache: false, 
    error: (xhr, ajaxOptions, thrownError) => { 
    debugger; 
    alert(xhr.statusText); 
    alert(thrownError); 
    }, 
    success: (json) => { 
    console.log(json); 
    if (!json || json.feeds === undefined || json.feeds.length === 0) { 
     window.alert("The returned output array length is ZERO."); 
    } else { 
     json.feeds.forEach(function (feed, index) { 
     var indexPlusOne = index + 1; 
     console.log(` 
The deails of ${indexPlusOne}th Object are: 
Created_at: ${feed.created_at} 
Entry_id: ${feed.entry_id} 
Field1: ${feed.field1} 
Field2: ${feed.field2}`); 
     }); 
    } 
    } 
}); 

,除非你真的需要使用cache選項我也將簡化它只是使用jQuery.getJSON代替jQuery.ajax

$.getJSON("https://api.thingspeak.com/channels/9/feeds.json?results=2") 
    .fail((req, status, err) => { 
    console.error(`AJAX call failed: ${err}`); 
    }) 
    .done((data) => { 
    console.log(data); 
    if (!data || data.feeds === undefined || data.feeds.length === 0) { 
     window.alert("The returned output array length is ZERO."); 
    } else { 
     data.feeds.forEach(function (feed, index) { 
     var indexPlusOne = index + 1; 
     console.log(` 
The deails of ${indexPlusOne}th Object are: 
Created_at: ${feed.created_at} 
Entry_id: ${feed.entry_id} 
Field1: ${feed.field1} 
Field2: ${feed.field2}`); 
     }); 
    } 
    }); 
+0

tq.it解決了我的問題問題@無用的代碼 – Anusha

0

答案爲以上問題如下。

$.ajax({ 
    url: " https://api.thingspeak.com/channels/9/feeds.json?results=2", 
    dataType: "json", 
    cache: false, 
    error: function(xhr, ajaxOptions, thrownError) { 
    debugger; 
    alert(xhr.statusText); 
    alert(thrownError); 
    }, 
    success: function(json1) { 
    console.log(json1); 
    json1.feeds.forEach(function(feed, i) { 
    console.log("\n The deails of " + i + "th Object are : \nCreated_at: " + feed.created_at + "\nEntry_id:" + feed.entry_id + "\nField1:" + feed.field1 + "\nField2:" + feed.field2); 
});