$.ajax を使用して、php ファイルの json リクエストを作成します。JSON 文字列は自分で作成したものではありません。外部 API です。だから私は決してそれをチャンスにすることはできません。
jQuery はすべての 1 次元エントリを通過します。'also_known_as' の場合、複数のエントリの 2 次元もチェックします。
問題は、2 次元にエントリがない場合に発生します。少なくとも 1 つの 2 次元エントリがあるかどうかを確認するにはどうすればよいですか?
私は試した
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;
}
});
});