调用/选择Json数据中的某些项目
Call/Select certain item in Json Data
我在一个个人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);
}
}
相关文章:
- 正在将数据主题添加到所有项目
- 谷歌图表堆叠柱状图.如何设置每个单独堆叠项目(数据系列)的样式
- 如何处理数据视图中项目的鼠标点击
- 在Knockout JS中搜索从DB加载数据的项目
- Firebase-正在从推送项目中检索数据
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- Grunt angularJs项目没有来自后端的依赖性's数据
- 显示离子项目中json文件中的数据
- jQuery.数据集's或获取's的项目不对
- 在没有JQuery的情况下将JSON数据获取到TVML项目中
- PHP根据用户选择的项目以模式加载数据
- 筛选源数据后的项目工具提示
- 在FB.api函数中创建一个数据项目
- 我无法在谷歌图表的代码 javascript 中获取 json 数据(项目 Python Hello Dashboard
- 如何在选择项目时使用 ajax 将数据提取到输入字段中
- 如何遍历项目列表并将其数据值推送到数组中
- 尝试使用PHP / Javascript构建一个可点击的索引,该索引显示单击的每个项目的数据
- 如何移除 DOJO 数据存储添加到我提取的项目中的所有额外字段
- addClass/removeClass 基于所选项目数据属性
- 如果我的模型是MVC3中的QueryTable,如何访问Javascript中的项目数据