首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用TMDB从JSON到HTML输出

使用TMDB从JSON到HTML输出
EN

Stack Overflow用户
提问于 2021-09-15 03:32:03
回答 1查看 226关注 0票数 0

我使用fetch API从TMBD中获取数据,我可以在控制台中这样显示.

代码语言:javascript
复制
fetch('https://api.themoviedb.org/3/movie/778?api_key=1234&append_to_response=videos,credits')
    .then(res => res.json())
    .then((out) => {
        console.log('Output: ', out);
}).catch(err => console.error(err));

JSON看起来像这样..。

代码语言:javascript
复制
{
"backdrop_path": "/8gSU1tLLdE5TWbcg9YDnR2lD0sO.jpg",
"belongs_to_collection": null,
"budget": 0,
"genres": [
    {
        "id": 99,
        "name": "Documentary"
    }
],
"homepage": "https://www.focusfeatures.com/roadrunner",
"id": 642732,
"imdb_id": "tt14512538",
"original_language": "en",
"original_title": "Roadrunner: A Film About Anthony Bourdain",
"overview": "An intimate, behind-the-scenes look at how an anonymous chef became a world-renowned cultural icon.",
"production_countries": [
    {
        "iso_3166_1": "US",
        "name": "United States of America"
    }
]

}

我现在需要把它放到一些简单的HTML中。例如,一些名为“original_title”和“概述”的变量,每个变量都在各自的div中.

代码语言:javascript
复制
<div>title outputs here</div>
<div>overview outputs here</div>

我知道这很简单,但我不能让JS工作。任何帮助都很感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-15 04:03:30

这是一个供您参考的示例代码。

代码语言:javascript
复制
let out={
  "adult": false,
  "backdrop_path": "/fCayJrkfRaCRCTh8GqN30f8oyQF.jpg",
  "belongs_to_collection": null,
  "budget": 63000000,
  "genres": [
    {
      "id": 18,
      "name": "Drama"
    }
  ],
  "homepage": "",
  "id": 550,
  "imdb_id": "tt0137523",
  "original_language": "en",
  "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": 0.5,
  "poster_path": null
};

function go(){
  let overViewDiv=document.getElementById("overViewDiv");
  let originalTitleDiv=document.getElementById("originalTitleDiv");
  
  overViewDiv.innerHTML=out.overview;
  originalTitleDiv.innerHTML=out.original_title;
}
代码语言:javascript
复制
Over View:
<div id="overViewDiv"></div>
Original Title:
<div id="originalTitleDiv"></div>
<button onclick="go()">Go</button>

在你的情况下

  1. 将每个div的id设置为示例代码.

  1. 并替换:

Console.log(“输出:”,out);

有:

让overViewDiv=document.getElementById("overViewDiv");让originalTitleDiv=document.getElementById("originalTitleDiv");overViewDiv.innerHTML=out.overview;originalTitleDiv.innerHTML=out.original_title;

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69186793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档