[英]How to loop through JSON object with $.each JQuery?
我想使用Star Wars api的 JSON數據進行實時搜索。 JSON包含一些objects
,而我想獲取的objects
是results
,它是一個包含objects
的array
,這些objects
具有屬性name
, height
, mass
和ect。
JSON的片段如下所示:
{
"count": 87,
"next": "http://swapi.co/api/people/?format=json&page=2",
"previous": null,
"results": [
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "http://swapi.co/api/planets/1/",
"films": [
"http://swapi.co/api/films/6/",
"http://swapi.co/api/films/3/",
"http://swapi.co/api/films/2/",
"http://swapi.co/api/films/1/",
"http://swapi.co/api/films/7/"
],
"species": [
"http://swapi.co/api/species/1/"
],
"vehicles": [
"http://swapi.co/api/vehicles/14/",
"http://swapi.co/api/vehicles/30/"
],
"starships": [
"http://swapi.co/api/starships/12/",
"http://swapi.co/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "http://swapi.co/api/people/1/"
}
]
}
我嘗試使用此代碼,但控制台顯示Uncaught TypeError: Cannot read property 'name' of undefined
(第7行)
$('#search').keyup(function() {
var searchField = $('#search').val();
var myExp = new RegExp(searchField, "i");
$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
var output = '<ul class="searchresults">';
$.each(data, function(key, val) {
if ((val.results.name.search(myExp) != -1) ||
(val.results.height.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ val.name +'</h2>';
output += '<p>'+ val.height +'</p>';
output += '</li>';
}
});
output += '</ul>';
$('#update').html(output);
});
});
我知道我的循環有問題,我嘗試尋找答案,但是我找不到正確的答案。 你能幫我么?
這是一個簡單的錯誤,您應該遍歷數據。
$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
$.each(data.results, function(key, val) {
console.log(val.name);
})
});
您的JSON包含:
因此,您必須獲得結果並對其進行迭代。
您需要像這樣遍歷data
對象的results
:
$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
$.each(data.results, function(key, value){
console.log("key", key);
console.log("value", value.name)
})
})
或您的情況:
$('#search').keyup(function() {
var searchField = $('#search').val();
var myExp = new RegExp(searchField, "i");
$.getJSON('http://swapi.co/api/people/?format=json', function(data) {
var output = '<ul class="searchresults">';
$.each(data.results, function(key, val) { //Only this line changes.
if ((val.results.name.search(myExp) != -1) ||
(val.results.height.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ val.name +'</h2>';
output += '<p>'+ val.height +'</p>';
output += '</li>';
}
});
output += '</ul>';
$('#update').html(output);
});
});
我認為您應該有這樣的東西:
output += '<h2>'+ val.results.name +'</h2>';
output += '<p>'+ val.results.height +'</p>';
與您的if
條件完全一樣
使您的$.each
循環如下:因為val.results
是json的數組格式。
$.each(data, function(key, val) {
var results = val.results;
$.each(results,function(index,value){
if ((value[index].name.search(myExp) != -1) ||
(value[index].height.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ value[index].name +'</h2>';
output += '<p>'+ value[index].height +'</p>';
output += '</li>';
}
});
});
希望它能正常工作。
或者簡單地
$.each(data.results,function(index,value){
if ((value[index].name.search(myExp) != -1) ||
(value[index].height.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ value[index].name +'</h2>';
output += '<p>'+ value[index].height +'</p>';
output += '</li>';
}
});
可能對您來說就足夠了。
嘗試這個
var a = //your json;
$.each(a,function(i,val){
if(val.results.length != 0){
$.each(val.results,function(k,value){
console.log(value.name)})
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.