调用/选择Json数据中的某些项目

Call/Select certain item in Json Data

本文关键字:项目 数据 选择 Json 调用      更新时间:2023-09-26

我在一个个人jQuery/ajax/json项目上工作,我正试图从TMDB API收集数据。我想让人们搜索某个词,比如电影《搏击俱乐部》。应该发生的是,看到与所输入的电影标题相匹配的电影海报。

我明白了:

. ajax({美元类型:"获得",数据类型:"jsonp",缓存:假的,url: https://api.themoviedb.org/3/search/movie?api_key=257654f35e3dff105574f97fb4b97035&查询=战斗+俱乐部",成功:toonTMDBresults//成功后,运行toonTMDBResults函数});

        function toonTMDBresults(tmdbJson) {
                console.log('TMDB:', tmdbJson);
                if ( 1 == 1 ) { // if 1 equals 1, so always run the following code... :)
                    for (var idx in tmdbJson.data) { //loop through the results
                        var results = tmdbJson.data[idx];
                        console.log(results);

                       console.log(results.results[0].id);  //take the ID of the first result in the array

                    }
                } else {
                    console.log('problem with the TMDB request:', json.meta.code);
                }

我想要发生的是我在我的控制台上看到电影的Id。如果我能够做到这一点,我将能够从json数据中选择poster_path,并在我的html中显示电影海报。

但问题是我没有得到我的控制台的ID,因为我要求,所以我做错了。

谢谢,马克

tmdbJson.data不存在。返回的对象是:

TMDB: Object {page: 1, results: Array[10], total_pages: 1, total_results: 10}

你可能是指在tmdbJson.results上迭代。这是你的函数的工作版本:

function toonTMDBresults(tmdbJson) {
            console.log('TMDB:', tmdbJson);
            console.log('first id: ', tmdbJson.results[0].id);
            console.log('first poster path: ', tmdbJson.results[0].poster_path);
            if ( 1 == 1 ) { // if 1 equals 1, so always run the following code... :)
                for (var idx in tmdbJson.results) { //loop through the results
                    var results = tmdbJson.results[idx];
                    console.log(results);

                   console.log(results.id);  //take the ID of the first result in the array

                }
            } else {
                console.log('problem with the TMDB request:', json.meta.code);
            }
}