0

我正在尝试解析这样的 JSON 数据:

   var baseUrl = 'https://api.themoviedb.org/3/movie/'
    var movieID = '550'
    var detailUrl = '&append_to_response=releases,trailers,credits&callback=?'
    var apiKey = '?api_key=Removed_For_Privacy'

上面带有 api 键的 url 包含返回此结果:

?({
    "adult": false,
    "backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
    "belongs_to_collection": null,
    "budget": 63000000,
    "genres": [
        {
            "id": 28,
            "name": "Action"
        },
        {
            "id": 18,
            "name": "Drama"
        },
        {
            "id": 53,
            "name": "Thriller"
        }
    ],
    "homepage": "",
    "id": 550,
    "imdb_id": "tt0137523",
    "original_title": "Fight Club",
    "overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
    "popularity": "10.2188172784825",
    "poster_path": "/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg",
    "production_companies": [
        {
            "name": "20th Century Fox",
            "id": 25
        },
        {
            "name": "Fox 2000 Pictures",
            "id": 711
        },
        {
            "name": "Regency Enterprises",
            "id": 508
        }
    ],
    "production_countries": [
        {
            "iso_3166_1": "DE",
            "name": "Germany"
        },
        {
            "iso_3166_1": "US",
            "name": "United States of America"
        }
    ],
    "release_date": "1999-10-14",
    "revenue": 100853753,
    "runtime": 139,
    "spoken_languages": [
        {
            "iso_639_1": "en",
            "name": "English"
        }
    ],
    "status": "Released",
    "tagline": "How much can you know about yourself if you've never been in a fight?",
    "title": "Fight Club",
    "vote_average": 7.6,
    "vote_count": 2787,
    "releases": {
        "countries": [
            {
                "iso_3166_1": "US",
                "certification": "R",
                "release_date": "1999-10-14"
            },
            {
                "iso_3166_1": "DE",
                "certification": "18",
                "release_date": "1999-11-10"
            },
            {
                "iso_3166_1": "GB",
                "certification": "18",
                "release_date": "1999-11-12"
            },
            {
                "iso_3166_1": "FR",
                "certification": "16",
                "release_date": "1999-11-10"
            },
            {
                "iso_3166_1": "TR",
                "certification": "",
                "release_date": "1999-12-10"
            },
            {
                "iso_3166_1": "BR",
                "certification": "feibris",
                "release_date": "1999-07-12"
            },
            {
                "iso_3166_1": "FI",
                "certification": "K-18",
                "release_date": "1999-11-12"
            },
            {
                "iso_3166_1": "BG",
                "certification": "c",
                "release_date": "2012-08-28"
            },
            {
                "iso_3166_1": "IT",
                "certification": "VM14",
                "release_date": "1999-10-29"
            }
        ]
    },
    "trailers": {
        "quicktime": [],
        "youtube": [
            {
                "name": "Trailer 1",
                "size": "HD",
                "source": "SUXWAEX2jlg",
                "type": "Trailer"
            }
        ]
    },
    "credits": {
        "cast": [
            {
                "id": 819,
                "name": "Edward Norton",
                "character": "The Narrator",
                "order": 0,
                "cast_id": 4,
                "profile_path": "/iUiePUAQKN4GY6jorH9m23cbVli.jpg"
            },
            {
                "id": 287,
                "name": "Brad Pitt",
                "character": "Tyler Durden",
                "order": 1,
                "cast_id": 5,
                "profile_path": "/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"
            },
            {
                "id": 1283,
                "name": "Helena Bonham Carter",
                "character": "Marla Singer",
                "order": 2,
                "cast_id": 6,
                "profile_path": "/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"
            },
            {
                "id": 7470,
                "name": "Meat Loaf",
                "character": "Robert 'Bob' Paulson",
                "order": 3,
                "cast_id": 7,
                "profile_path": "/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"
            },
            {
                "id": 7499,
                "name": "Jared Leto",
                "character": "Angel Face",
                "order": 4,
                "cast_id": 30,
                "profile_path": "/msugySeTCyCmlRWtyB6sMixTQYY.jpg"
            },
            {
                "id": 7471,
                "name": "Zach Grenier",
                "character": "Richard Chesler",
                "order": 5,
                "cast_id": 31,
                "profile_path": "/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"
            },
            {
                "id": 7497,
                "name": "Holt McCallany",
                "character": "The Mechanic",
                "order": 6,
                "cast_id": 32,
                "profile_path": "/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"
            },
            {
                "id": 7498,
                "name": "Eion Bailey",
                "character": "Ricky",
                "order": 7,
                "cast_id": 33,
                "profile_path": "/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"
            }
        ],
        "crew": [
            {
                "id": 7469,
                "name": "Jim Uhls",
                "department": "Writing",
                "job": "Author",
                "profile_path": null
            },
            {
                "id": 7474,
                "name": "Ross Grayson Bell",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 7475,
                "name": "Ceán Chaffin",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 1254,
                "name": "Art Linson",
                "department": "Production",
                "job": "Producer",
                "profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
            },
            {
                "id": 7477,
                "name": "John King",
                "department": "Sound",
                "job": "Original Music Composer",
                "profile_path": null
            },
            {
                "id": 7478,
                "name": "Michael Simpson",
                "department": "Sound",
                "job": "Original Music Composer",
                "profile_path": null
            },
            {
                "id": 7479,
                "name": "Jeff Cronenweth",
                "department": "Camera",
                "job": "Director of Photography",
                "profile_path": null
            },
            {
                "id": 7480,
                "name": "James Haygood",
                "department": "Editing",
                "job": "Editor",
                "profile_path": null
            },
            {
                "id": 7481,
                "name": "Laray Mayfield",
                "department": "Production",
                "job": "Casting",
                "profile_path": null
            },
            {
                "id": 1303,
                "name": "Alex McDowell",
                "department": "Art",
                "job": "Production Design",
                "profile_path": null
            },
            {
                "id": 7763,
                "name": "Ren Klyce",
                "department": "Sound",
                "job": "Sound Editor",
                "profile_path": null
            },
            {
                "id": 7764,
                "name": "Richard Hymns",
                "department": "Sound",
                "job": "Sound Editor",
                "profile_path": null
            },
            {
                "id": 7467,
                "name": "David Fincher",
                "department": "Directing",
                "job": "Director",
                "profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
            },
            {
                "id": 7468,
                "name": "Chuck Palahniuk",
                "department": "Writing",
                "job": "Novel",
                "profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
            }
        ]
    }
})

我用它来解析它,但是我没有运气

$(document).ready(function() {

  $.ajax({
    url: baseUrl + movieID +apiKey +detailUrl,
    dataType: "jsonp",
    success: getGenres,
  });
});


function getGenres(data) {
var entries = data
genre = 0,
genre_list = '';


for (genre = 0; genre < entries.genres.name.length; genre++) {
    genre_list.push(entries.genres.name[genre]);
}

    document.getElementById('Genres').innerHTML = genre_list.join(', ');

请帮忙

4

2 回答 2

1

entries.genres是一个数组。它没有.name财产。您应该在浏览器的开发人员控制台中收到错误,以.length访问undefined.

{
    "adult": false,
    "backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
    "belongs_to_collection": null,
    "budget": 63000000,
    "genres": [
        {
            "id": 28,
            "name": "Action"
        },
        {
            "id": 18,
            "name": "Drama"
        },
        {
            "id": 53,
            "name": "Thriller"
        }
    ],
 ...
 }

所以你需要迭代,然后如果你想要的话,entries.genres推送当前流派的。.name

for (genre = 0; genre < entries.genres.length; genre++) {
    genre_list.push(entries.genres[genre].name);
}


另一方面,您有两个隐式全局变量。

var entries = data
genre = 0,
genre_list = '';

通过忘记 之后的逗号var entries = data,接下来的两行将隐式创建全局变量,因为它们不是var语句的一部分。

这就是为什么我总是在变量声明中使用前导逗号。缺少逗号时会很明显。

var entries = data
,   genre = 0
,   genre_list = '';
于 2014-01-25T02:50:49.393 回答
-2

测试.php

   <?php
        echo 'getGenres({
    "adult": false,
    "backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
    "belongs_to_collection": null,
    "budget": 63000000,
    "genres": [
        {
            "id": 28,
            "name": "Action"
        },
        {
            "id": 18,
            "name": "Drama"
        },
        {
            "id": 53,
            "name": "Thriller"
        }
    ],
    "homepage": "",
    "id": 550,
    "imdb_id": "tt0137523",
    "original_title": "Fight Club",
    "overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
    "popularity": "10.2188172784825",
    "poster_path": "/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg",
    "production_companies": [
        {
            "name": "20th Century Fox",
            "id": 25
        },
        {
            "name": "Fox 2000 Pictures",
            "id": 711
        },
        {
            "name": "Regency Enterprises",
            "id": 508
        }
    ],
    "production_countries": [
        {
            "iso_3166_1": "DE",
            "name": "Germany"
        },
        {
            "iso_3166_1": "US",
            "name": "United States of America"
        }
    ],
    "release_date": "1999-10-14",
    "revenue": 100853753,
    "runtime": 139,
    "spoken_languages": [
        {
            "iso_639_1": "en",
            "name": "English"
        }
    ],
    "status": "Released",
    "tagline": "How much can you know about yourself if youve never been in a fight?",
    "title": "Fight Club",
    "vote_average": 7.6,
    "vote_count": 2787,
    "releases": {
        "countries": [
            {
                "iso_3166_1": "US",
                "certification": "R",
                "release_date": "1999-10-14"
            },
            {
                "iso_3166_1": "DE",
                "certification": "18",
                "release_date": "1999-11-10"
            },
            {
                "iso_3166_1": "GB",
                "certification": "18",
                "release_date": "1999-11-12"
            },
            {
                "iso_3166_1": "FR",
                "certification": "16",
                "release_date": "1999-11-10"
            },
            {
                "iso_3166_1": "TR",
                "certification": "",
                "release_date": "1999-12-10"
            },
            {
                "iso_3166_1": "BR",
                "certification": "feibris",
                "release_date": "1999-07-12"
            },
            {
                "iso_3166_1": "FI",
                "certification": "K-18",
                "release_date": "1999-11-12"
            },
            {
                "iso_3166_1": "BG",
                "certification": "c",
                "release_date": "2012-08-28"
            },
            {
                "iso_3166_1": "IT",
                "certification": "VM14",
                "release_date": "1999-10-29"
            }
        ]
    },
    "trailers": {
        "quicktime": [],
        "youtube": [
            {
                "name": "Trailer 1",
                "size": "HD",
                "source": "SUXWAEX2jlg",
                "type": "Trailer"
            }
        ]
    },
    "credits": {
        "cast": [
            {
                "id": 819,
                "name": "Edward Norton",
                "character": "The Narrator",
                "order": 0,
                "cast_id": 4,
                "profile_path": "/iUiePUAQKN4GY6jorH9m23cbVli.jpg"
            },
            {
                "id": 287,
                "name": "Brad Pitt",
                "character": "Tyler Durden",
                "order": 1,
                "cast_id": 5,
                "profile_path": "/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"
            },
            {
                "id": 1283,
                "name": "Helena Bonham Carter",
                "character": "Marla Singer",
                "order": 2,
                "cast_id": 6,
                "profile_path": "/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"
            },
            {
                "id": 7470,
                "name": "Meat Loaf",
                "character": "Robert Bob Paulson",
                "order": 3,
                "cast_id": 7,
                "profile_path": "/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"
            },
            {
                "id": 7499,
                "name": "Jared Leto",
                "character": "Angel Face",
                "order": 4,
                "cast_id": 30,
                "profile_path": "/msugySeTCyCmlRWtyB6sMixTQYY.jpg"
            },
            {
                "id": 7471,
                "name": "Zach Grenier",
                "character": "Richard Chesler",
                "order": 5,
                "cast_id": 31,
                "profile_path": "/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"
            },
            {
                "id": 7497,
                "name": "Holt McCallany",
                "character": "The Mechanic",
                "order": 6,
                "cast_id": 32,
                "profile_path": "/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"
            },
            {
                "id": 7498,
                "name": "Eion Bailey",
                "character": "Ricky",
                "order": 7,
                "cast_id": 33,
                "profile_path": "/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"
            }
        ],
        "crew": [
            {
                "id": 7469,
                "name": "Jim Uhls",
                "department": "Writing",
                "job": "Author",
                "profile_path": null
            },
            {
                "id": 7474,
                "name": "Ross Grayson Bell",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 7475,
                "name": "Ceán Chaffin",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 1254,
                "name": "Art Linson",
                "department": "Production",
                "job": "Producer",
                "profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
            },
            {
                "id": 7477,
                "name": "John King",
                "department": "Sound",
                "job": "Original Music Composer",
                "profile_path": null
            },
            {
                "id": 7478,
                "name": "Michael Simpson",
                "department": "Sound",
                "job": "Original Music Composer",
                "profile_path": null
            },
            {
                "id": 7479,
                "name": "Jeff Cronenweth",
                "department": "Camera",
                "job": "Director of Photography",
                "profile_path": null
            },
            {
                "id": 7480,
                "name": "James Haygood",
                "department": "Editing",
                "job": "Editor",
                "profile_path": null
            },
            {
                "id": 7481,
                "name": "Laray Mayfield",
                "department": "Production",
                "job": "Casting",
                "profile_path": null
            },
            {
                "id": 1303,
                "name": "Alex McDowell",
                "department": "Art",
                "job": "Production Design",
                "profile_path": null
            },
            {
                "id": 7763,
                "name": "Ren Klyce",
                "department": "Sound",
                "job": "Sound Editor",
                "profile_path": null
            },
            {
                "id": 7764,
                "name": "Richard Hymns",
                "department": "Sound",
                "job": "Sound Editor",
                "profile_path": null
            },
            {
                "id": 7467,
                "name": "David Fincher",
                "department": "Directing",
                "job": "Director",
                "profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
            },
            {
                "id": 7468,
                "name": "Chuck Palahniuk",
                "department": "Writing",
                "job": "Novel",
                "profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
            }
        ]
    }
})';
exit;

?>

javascript代码:

<script language="javascript">
    jq(document).ready(function() {
     jq.ajax({
    url: 'test.php',
    dataType: "jsonp",
        });  });
        function getGenres(data){ 
            alert(data.genres.length);
        }  
    </script>

您的 json 响应包含单引号 (') 示例 'bob' 而您不是标准的,因此请替换为 \' 然后解析您的 json 响应。

作为参考,请检查JSON 响应中的 jQuery 单引号

从 json 响应的开头和结尾删除 ?( 和 ) 并从中删除 ' 并检查

var obj = jQuery.parseJSON( '{"adult":false,"backdrop_path":"/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg","belongs_to_collection":null,"budget":63000000,"genres":[{"id":28,"name":"Action"},{"id":18,"name":"Drama"},{"id":53,"name":"Thriller"}],"homepage":"","id":550,"imdb_id":"tt0137523","original_title":"Fight Club","overview":"A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground fight clubs forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.","popularity":"10.2188172784825","poster_path":"/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg","production_companies":[{"name":"20th Century Fox","id":25},{"name":"Fox 2000 Pictures","id":711},{"name":"Regency Enterprises","id":508}],"production_countries":[{"iso_3166_1":"DE","name":"Germany"},{"iso_3166_1":"US","name":"United States of America"}],"release_date":"1999-10-14","revenue":100853753,"runtime":139,"spoken_languages":[{"iso_639_1":"en","name":"English"}],"status":"Released","tagline":"How much can you know about yourself if youve never been in a fight?","title":"Fight Club","vote_average":7.6,"vote_count":2787,"releases":{"countries":[{"iso_3166_1":"US","certification":"R","release_date":"1999-10-14"},{"iso_3166_1":"DE","certification":"18","release_date":"1999-11-10"},{"iso_3166_1":"GB","certification":"18","release_date":"1999-11-12"},{"iso_3166_1":"FR","certification":"16","release_date":"1999-11-10"},{"iso_3166_1":"TR","certification":"","release_date":"1999-12-10"},{"iso_3166_1":"BR","certification":"feibris","release_date":"1999-07-12"},{"iso_3166_1":"FI","certification":"K-18","release_date":"1999-11-12"},{"iso_3166_1":"BG","certification":"c","release_date":"2012-08-28"},{"iso_3166_1":"IT","certification":"VM14","release_date":"1999-10-29"}]},"trailers":{"quicktime":[],"youtube":[{"name":"Trailer 1","size":"HD","source":"SUXWAEX2jlg","type":"Trailer"}]},"credits":{"cast":[{"id":819,"name":"Edward Norton","character":"The Narrator","order":0,"cast_id":4,"profile_path":"/iUiePUAQKN4GY6jorH9m23cbVli.jpg"},{"id":287,"name":"Brad Pitt","character":"Tyler Durden","order":1,"cast_id":5,"profile_path":"/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"},{"id":1283,"name":"Helena Bonham Carter","character":"Marla Singer","order":2,"cast_id":6,"profile_path":"/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"},{"id":7470,"name":"Meat Loaf","character":"Robert Bob Paulson","order":3,"cast_id":7,"profile_path":"/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"},{"id":7499,"name":"Jared Leto","character":"Angel Face","order":4,"cast_id":30,"profile_path":"/msugySeTCyCmlRWtyB6sMixTQYY.jpg"},{"id":7471,"name":"Zach Grenier","character":"Richard Chesler","order":5,"cast_id":31,"profile_path":"/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"},{"id":7497,"name":"Holt McCallany","character":"The Mechanic","order":6,"cast_id":32,"profile_path":"/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"},{"id":7498,"name":"Eion Bailey","character":"Ricky","order":7,"cast_id":33,"profile_path":"/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"}],"crew":[{"id":7469,"name":"Jim Uhls","department":"Writing","job":"Author","profile_path":null},{"id":7474,"name":"Ross Grayson Bell","department":"Production","job":"Producer","profile_path":null},{"id":7475,"name":"Ceán Chaffin","department":"Production","job":"Producer","profile_path":null},{"id":1254,"name":"Art Linson","department":"Production","job":"Producer","profile_path":"/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"},{"id":7477,"name":"John King","department":"Sound","job":"Original Music Composer","profile_path":null},{"id":7478,"name":"Michael Simpson","department":"Sound","job":"Original Music Composer","profile_path":null},{"id":7479,"name":"Jeff Cronenweth","department":"Camera","job":"Director of Photography","profile_path":null},{"id":7480,"name":"James Haygood","department":"Editing","job":"Editor","profile_path":null},{"id":7481,"name":"Laray Mayfield","department":"Production","job":"Casting","profile_path":null},{"id":1303,"name":"Alex McDowell","department":"Art","job":"Production Design","profile_path":null},{"id":7763,"name":"Ren Klyce","department":"Sound","job":"Sound Editor","profile_path":null},{"id":7764,"name":"Richard Hymns","department":"Sound","job":"Sound Editor","profile_path":null},{"id":7467,"name":"David Fincher","department":"Directing","job":"Director","profile_path":"/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"},{"id":7468,"name":"Chuck Palahniuk","department":"Writing","job":"Novel","profile_path":"/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"}]}}' );

alert( obj.genres.length );  

显示 3 的警报消息,因此您的 json 响应无效

于 2014-01-25T03:02:11.893 回答