如何读取和显示json数据

时间:2013-02-23 15:52:03

标签: jquery json

我有以下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的{​​{1}}对象中。第二个profile。第三个评级,即titlecritics_score,位于audience_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图像时,我先使用poster函数内部,然后创建几个空的img标记。每个函数内部的其他两个相同。 如果有的话,有没有更好的方法来阅读上面的json数据?

0 个答案:

没有答案