申请循环json响应(多维)来获取值

时间:2016-04-05 08:21:48

标签: javascript jquery json

<h1>Fox News is getting really good at spotting Ted Cruz’s lies.</h1>
<div class="newbody body parsys">
  <div class="parbase image slate_image section">
    <div class="">
      <figure class="image inline ">
        <img src_tag_name="src" title="Ted Cruz " alt="Ted Cruz " src="http://www.slate.com/content/dam/slate/articles/news_and_politics/politics/2016/01/160129_pol_ted-cruz_1024.jpg.CROP.promo-xlarge2.jpg">
        <figcaption class="caption"><span>Ted Cruz speaks during the Republican presidential debate sponsored by Fox News in Des Moines, Iowa, on Jan. 28, 2016.</span>
        </figcaption>
        <p class="credit">Jim Watson/Getty Images</p>
      </figure>
    </div>
  </div>
  <div class="text text-1 parbase section">
    <p>In Thursday night’s Republican presidential debate, moderator Megyn Kelly <a href="https://www.washingtonpost.com/news/the-fix/wp/2016/01/28/7th-republican-debate-transcript-annotated-who-said-what-and-what-it-meant/">grilled Sen. Ted Cruz</a> about his role in the Senate’s 2013 fight over immigration reform. As <strong><em>Slate</em></strong> recently explained, that fight is <a href="http://www.slate.com/articles/news_and_politics/politics/2016/01/the_definitive_timeline_of_what_ted_cruz_said_and_did_in_the_2013_immigration.html?wpsrc=sp_all_article_storypromo">an excellent episode to study</a> if you want to understand Cruz’s <a href="http://www.slate.com/articles/news_and_politics/cover_story/2016/01/ted_cruz_may_be_the_most_gifted_liar_ever_to_run_for_president.html">peculiar views about truth and deception</a>. By pressing Cruz about discrepancies between what he said then and what he says now, Kelly forced him to show the public, on live television, how he thinks about honesty. It wasn’t pretty.</p>
  </div>
  <section class="about-the-author fancy inline with-head">
    <div class="author-bio">
      <p>Will Saletan writes about politics, science, technology, and other stuff for <b><i>Slate</i></b>. He’s the author of <i><a target="_blank" href="http://rads.stackoverflow.com/amzn/click/0520243366">Bearing Right</a></i>.<a href="http://rads.stackoverflow.com/amzn/click/0520243366"><i><br></i></a>
      </p>
    </div>
  </section>
  <!-- /.about-the-author -->
  <div class="text-2 text parbase section">
    <p>Kelly showed the debate audience four video clips. In the clips, taken from Cruz’s speeches during the 2013 fight, he earnestly appealed for a compromise that would allow undocumented immigrants to earn legal status—as prescribed by a bill the Senate was considering—but not citizenship. The key to the compromise was an amendment, offered by Cruz, that would have <a href="http://www.judiciary.senate.gov/imo/media/doc/Cruz3-(DAV13373).pdf">closed the path to citizenship</a>. Cruz now denies that he ever supported legalization. Here’s how he tried to explain the video clips, and what the exchange revealed about him.</p>
  </div>
  <div class="text-3 text parbase section">
    <p><strong>1. He lies.</strong> Kelly’s first clip, taken from Cruz’s speech in the Senate Judiciary Committee on May 9, 2013, showed him saying, “<a href="http://www.cruz.senate.gov/?p=press_release&amp;id=140">I don’t want this bill to be voted down</a>.” The second clip, taken from his speech to the committee on May 21, 2013, showed him saying, “<a href="http://www.cruz.senate.gov/?p=press_release&amp;id=128">If [my] amendment were to pass, the chances of this bill passing into law would increase dramatically</a>.” The third clip, taken from Cruz’s <a href="http://web.princeton.edu/sites/jmadison/calendar/flash/Cruz.html">remarks at Princeton University</a> on May 31, 2013, showed him saying: “I believe if the amendments I introduced were adopted, that the bill would pass. And my effort in introducing them was to find a solution that reflected common ground and that fixed the problem.”</p>
  </div>
  <div class="text-13 text parbase section">
    <p>In his closing statement, Cruz put the issue squarely: “The central question in this race is trust.” Indeed it is. Cruz is running for president as the candidate you can believe in, the man who says what he means and does what he says. It’s a good slogan. It’s just not true of Cruz.</p>
  </div>
</div>

以上是我的json回复。我想通过使用jQuery来获取标题值。

我尝试了下面的代码...我每次在用户在文本框中插入字符时发送请求({ "Search": [{ "Title": "Batman Begins", "Year": "2005", "imdbID": "tt0372784", "Type": "movie", "Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg" }, { "Title": "Batman", "Year": "1989", "imdbID": "tt0096895", "Type": "movie", "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwNjAyODIyMF5BMl5BanBnXkFtZTYwNDMwMDk2._V1_SX300.jpg" }], "totalResults": "291", "Response": "True" } 上):

keyup

2 个答案:

答案 0 :(得分:0)

您可以使用Jquery.each()函数迭代数组和对象。 它将数组或对象作为第一个参数和回调函数。 因为响应是以对象的形式存在的,所以如果你想在之前实现一些逻辑,你需要在持有对象的数组上运行另一个Jquery.each(),或者只是在数组上运行Jquery.each()。自我:

以下是在控制台上打印标题的示例代码:

$.getJSON(url, { get_param: 'value' }, function(data) {
            $.each(data.Search, function(index, value) {
              console.log(value.Title);
            });

        });

这是一个JSFiddle

答案 1 :(得分:0)

$.each(data.Search, function(obj){          
   alert(obj.Title);
});