0

我有以下 json 数据。

{
  "movies": [{
  "id": "770687943",
  "title": "Harry Potter and the Deathly Hallows - Part 2",
  "year": 2011,
  "mpaa_rating": "PG-13",
   "runtime": 130,
   "critics_consensus": "Thrilling, powerfully acted, and visually dazzling, Deathly Hallows Part II brings the Harry Potter franchise to a satisfying -- and suitably magical -- conclusion.",
  "release_dates": {"theater": "2011-07-15"},
  "ratings": {
  "critics_rating": "Certified Fresh",
  "critics_score": 97,
  "audience_rating": "Upright",
  "audience_score": 93
},
"synopsis": "Harry Potter and the Deathly Hallows - Part 2, is the final adventure in the Harry Potter film series. The much-anticipated motion picture event is the second of two full-length parts. In the epic finale, the battle between the good and evil forces of the wizarding world escalates into an all-out war. The stakes have never been higher and no one is safe. But it is Harry Potter who may be called upon to make the ultimate sacrifice as he draws closer to the climactic showdown with Lord Voldemort. It all ends here. -- (C) Warner Bros",
"posters": {
  "thumbnail": "http://content8.flixster.com/movie/11/15/86/11158674_mob.jpg",
  "profile": "http://content8.flixster.com/movie/11/15/86/11158674_pro.jpg",
  "detailed": "http://content8.flixster.com/movie/11/15/86/11158674_det.jpg",
  "original": "http://content8.flixster.com/movie/11/15/86/11158674_ori.jpg"
},
"abridged_cast": [
  {
    "name": "Daniel Radcliffe",
    "characters": ["Harry Potter"]
  },
  {
    "name": "Rupert Grint",
    "characters": [
      "Ron Weasley",
      "Ron Wesley"
    ]
  },
  {
    "name": "Emma Watson",
    "characters": ["Hermione Granger"]
  },
  {
    "name": "Helena Bonham Carter",
    "characters": ["Bellatrix Lestrange"]
  },
  {
    "name": "Ralph Fiennes",
    "characters": ["Lord Voldemort"]
  }
],
"alternate_ids": {"imdb": "1201607"},
"links": {
  "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/770687943.json",
  "alternate": "http://www.rottentomatoes.com/m/harry_potter_and_the_deathly_hallows_part_2/",
  "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/770687943/cast.json",
  "clips": "http://api.rottentomatoes.com/api/public/v1.0/movies/770687943/clips.json",
  "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/770687943/reviews.json",
  "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/770687943/similar.json"
}
}],
 "links": {
  "self": "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit=1&country=us",
  "alternate": "http://www.rottentomatoes.com/movie/box_office.php"
 },
"link_template":  "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit={num-results}&country={country-code}"
}

从上面的 json 输出中我唯一需要的是image内部poster对象名为profile. 第二title。第三个评级即critics_scoreaudience_score哪些在rating对象内部。我使用underscorejs模板引擎阅读本文的方式如下

<% _.each(movies, function(movie) { %>
<% var imdb_id; %>
<div class="boxone">
    <% _.each(movie, function (poster) { %>
        <img class="poster" src="<%= poster.profile %>" />
    <% }); %>
    <h4><%= movie.title %></h4>
    <p>
        <span>Up vote: </span>
        <% _.each(movie, function (rating) { %>
            <%= rating.audience_score %>
        <% }); %>
    </p>

    <p>
        <span>Down vote: </span>
        <% _.each(movie, function (rating) { %>
            <%= rating.critics_score %>
        <% }); %>
    </p>

    <p>
        <% _.each(movie, function (alternate_ids) { %>
            <% imdb_id = alternate_ids.imdb; %>
            <%= imdb_id %>
        <% }); %>
        <span>ID: </span><%= imdb_id %>
        <a href="http://www.imdb.com/title/tt<% imdb_id %>">IMDB</a>
    </p>

</div>
<% }); %>

问题

我当前代码的问题是,当我想在对象profile内部读取和显示图像时,我首先使用内部函数,然后它会创建几个空的 img 标签。每个函数中的其他两个也是如此。如果是的话,有没有更好的方法来读取上面的json数据?postereach()

4

0 回答 0