【问题标题】:Call/Select certain item in Json Data调用/选择 Json 数据中的某些项目
【发布时间】:2014-08-24 04:10:24
【问题描述】:

我正在开发一个个人 jQuery/ajax/json 项目,我正在尝试从 TMDB API 收集数据。我希望人们在哪里搜索某个词,例如电影搏击俱乐部。应该发生的是看到与输入的电影标题匹配的电影海报。

到目前为止我得到了这个:

$.ajax({ 类型:“获取”, 数据类型:“jsonp”, 缓存:假, 网址:“https://api.themoviedb.org/3/search/movie?api_key=257654f35e3dff105574f97fb4b97035&query=fight+club”, 成功:卡通TMDB结果 // 成功时,运行函数 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,所以我做错了。

谢谢,

标记

【问题讨论】:

    标签: javascript jquery ajax json api


    【解决方案1】:

    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);
                }
    }
    

    【讨论】:

    • 非常感谢 :) 现在我正在尝试做同样的事情,但只是针对第一个 poster_path。但是 console.log(results[1].poster_path);似乎不起作用。如何仅检索 1 个结果而不是 10 个结果? (10 = 默认)。
    • 我添加了一个打印第一个 id 和 poster_path 的示例(在 if 之前)。请记住,在 javascript 中,数组从索引 0 开始,而不是 1。还请记住,我们定义的 results 变量指的是单个电影。要自己处理一部电影,请查看示例。您需要访问tmdbJson.results 并对其进行子索引。
    • 非常感谢。现在可以工作了。现在我试图创建一个 div 并将电影的图像放在那里。尝试使用这个: $('#posterarea').append('image.tmdb.org/t/p/w500/' + posterpath);但这不起作用。即使我这样写: 我收到一个错误:Users/Mark/Desktop/twitter/image.tmdb .org/t/p/w500/hpt3aa5i0TrSAnEdl3VJrRrje8C.jpg net::ERR_FILE_NOT_FOUND 这很奇怪,因为如果您访问该网址,您确实会看到正确的图像。
    • 你忘记了 http :-) 如果你不放它,路径将是相对于你的站点的。应该是 image.tmdb.org/t/p/w500/hpt3aa5i0TrSAnEdl3VJrRrje8C.jpg" id="posterarea" />
    • 是的,这行得通,谢谢:) 但是当我尝试从 json 数据中放置图像时,它没有。我想我做错了什么。我用 创建了一个 div。所以添加正确的 url 我在 jQuery 中做了这个: $('#posterarea' > '').append('image.tmdb.org/t/p/w500' + posterpath);我不再知道如何在 $(...) 部分中添加两个元素。比如,div“posterarea”中的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2012-08-28
    相关资源
    最近更新 更多