我使用 $.ajax 来获取 php 文件的 json 请求。JSON 字符串不是我自己构建的。它是一个外部 API。所以我不能以任何方式冒险。
jQuery 遍历所有一维条目。对于“also_known_as”,它还会检查第二维是否有多个条目。
如果二维中没有任何条目,则会出现问题。如何检查是否至少有一个二维条目?
我试过
if(typeof results[i].also_known_as[0] === "undefined")
和
if($.type(results[i].also_known_as[0]) === "undefined")
(也没有“”)
有任何想法吗?
JSON 看起来像这样:
[
{
"plot": "Former cop ...",
"genres": [
"Action",
"Crime",
"Thriller"
],
"rated": "PG-13",
"rating": 7.3,
"language": [
"English",
"Portuguese",
"Spanish"
],
"rating_count": 181888,
"country": [
"USA"
],
"release_date": 20110429,
"title": "Fast Five",
"year": 2011,
"filming_locations": "Rice, California, USA",
"imdb_id": "tt1596343",
"directors": [
"Justin Lin"
],
"writers": [
"Chris Morgan",
"Gary Scott Thompson"
],
"actors": [
"Vin Diesel",
"Paul Walker",
"Jordana Brewster",
"Tyrese Gibson",
"Ludacris",
"Matt Schulze",
"Sung Kang",
"Gal Gadot",
"Tego Calderon",
"Don Omar",
"Joaquim de Almeida",
"Dwayne Johnson",
"Elsa Pataky",
"Michael Irby",
"Fernando Chien"
],
"plot_simple": "Dominic ...",
"poster": {
"imdb": "http://ia.media-imdb.com/images/M/MV5BMTUxNTk5MTE0OF5BMl5BanBnXkFtZTcwMjA2NzY3NA@@._V1_SY317_CR0,0,214,317_.jpg",
"cover": "http://imdb-poster.b0.upaiyun.com/001/596/343.jpg!cover?_upt=6182835c1382348170"
},
"runtime": [
"130 min"
],
"type": "M",
"imdb_url": "http://www.imdb.com/title/tt1596343/",
"also_known_as": [
{
"country": "Argentina",
"title": "Rápidos y furiosos 5in control"
},
{
"country": "Australia",
"title": "Fast & Furious 5"
},
{
"remarks": [
"Bulgarian title"
],
"country": "Bulgaria",
"title": "Бързи и яростни 5: Удар в Рио"
}
]
},
{
"plot": "Former cop ...",
"genres": [
"Action",
"Crime",
"Thriller"
],
"rated": "PG-13",
"rating": 7.3,
"language": [
"English",
"Portuguese",
"Spanish"
],
"rating_count": 181888,
"country": [
"USA"
],
"release_date": 20110429,
"title": "Fast Five",
"year": 2011,
"filming_locations": "Rice, California, USA",
"imdb_id": "tt1596343",
"directors": [
"Justin Lin"
],
"writers": [
"Chris Morgan",
"Gary Scott Thompson"
],
"actors": [
"Vin Diesel",
"Paul Walker",
"Jordana Brewster",
"Tyrese Gibson",
"Ludacris",
"Matt Schulze",
"Sung Kang",
"Gal Gadot",
"Tego Calderon",
"Don Omar",
"Joaquim de Almeida",
"Dwayne Johnson",
"Elsa Pataky",
"Michael Irby",
"Fernando Chien"
],
"plot_simple": "Dominic ...",
"poster": {
"imdb": "http://ia.media-imdb.com/images/M/MV5BMTUxNTk5MTE0OF5BMl5BanBnXkFtZTcwMjA2NzY3NA@@._V1_SY317_CR0,0,214,317_.jpg",
"cover": "http://imdb-poster.b0.upaiyun.com/001/596/343.jpg!cover?_upt=6182835c1382348170"
},
"runtime": [
"130 min"
],
"type": "M",
"imdb_url": "http://www.imdb.com/title/tt1596343/"
}
]
这是我的 jQuery 函数的片段:
$.each(results, function(i, item) {
var id = results[i].imdb_id;
var title = results[i].title;
var also_known_as = '';
var also_known_as_prio = ''; //1 = Switzerland, 2 = Germany, 3 = Austria ... Geringere Prio -> wichtiger -> Überschreibt weniger wichtige
$.each(results[i].also_known_as, function(ii, iitem) {
switch (results[i].also_known_as[ii].country) {
case 'Switzerland':
also_known_as = results[i].also_known_as[ii].title;
also_known_as_prio = 1;
break;
case 'Germany':
if (also_known_as_prio != 1) {
also_known_as = results[i].also_known_as[ii].title;
also_known_as_prio = 2;
}
break;
case 'Austria':
if (also_known_as_prio != 1 && also_known_as_prio != 2) {
also_known_as = results[i].also_known_as[ii].title;
also_known_as_prio = 3;
}
break;
}
});
});