我正在尝试使用url来获取json数据。现在我需要解析Json数据以仅从url获取提要数据。我正在添加下面的代码和json数据。任何人都可以帮助我我怎么能解析来自以下json output.thankyou的数据。
JSON DATA:
{
"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 :(得分:2)
json1
不是一个数组,它是一个对象。您需要访问对象的.feeds
属性才能访问Feed数组。像这样:
$.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}`);
});
}
});
答案 1 :(得分:1)
只需解析个别Feed的Feed数组:
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);
});
答案 2 :(得分:0)
这里的问题是你尝试使用字符串并尝试获取长度(json1.length
),但是引擎盖下的jquery
为你返回JSON对象(因为你已经指定了{{1} }})。
所以你必须不是用字符串而是用对象!在你的例子中,你有一个带有属性dataType: "json"
的简单对象,因此你只需要feeds
中的遍历数组。
这是我的示例(我刚刚更新了json1.feeds
):
success callback
答案 3 :(得分: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);
});