0

私は現在 ESPN API を使用している初心者です。API をクエリした後、記事の見出し、リンク、画像を表示したいと考えています。見出ししか表示できませんでした。画像とリンクをループするにはどうすればよいですか?

  <!doctype html>
  <html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Work</title>

    <script src="jquery-1.9.1.js"></script>
  </head>
  <body>
    <script>
    (function() {
      var espnAPI = "http://api.espn.com/v1/sports/soccer/news/headlines/top/?apikey=*************&callback=?";
      $.getJSON( espnAPI, {

        format: "json"
      })
        .done(function( data ) {
          var ol = $("<ol/>");
        $.each( data.headlines, function() {
      var h2 = $( "<h2/>" ).text(this.headline);

      ol.append(h2)
      });
      $("body").append(ol);
      });
  })();

  </script>

  </body>
  </html>

これは、Chrome の開発者ツールから取得した応答本文の一部です。

{
"timestamp": "2013-10-21T08:43:40Z",
"resultsOffset": 0,
"status": "success",
"resultsLimit": 10,
"resultsCount": 43,
"headlines": [{
    "headline": "Townsend stars as Spurs beat Villa",
    "keywords": [],
    "lastModified": "2013-10-20T21:15:25Z",
    "audio": [],
    "premium": false,
    "mobileStory": "",
    "links": {
        "api": {
            "news": {
                "href": "http://api.espn.com/v1/sports/news/1589010?region=GB"
            }
        },
        "web": {
            "href": "http://espnfc.com/uk/en/report/367415/townsend-stars-spurs-beat-villa?ex_cid=espnapi_public"
        },
        "mobile": {
            "href": "http://m.espn.go.com/soccer/gamecast?gameId=367415&lang=EN&ex_cid=espnapi_public"
        }
    },
    "type": "snReport",
    "related": [],
    "id": 1589010,
    "story": "",
    "linkText": "Aston Villa 0-2 Spurs: Townsend stars",
    "images": [{
        "height": 360,
        "alt": "Andros Townsend is mobbed after his cross inadvertently gave Spurs the lead at Aston Villa.",
        "width": 640,
        "name": "Spurs celeb Andros Townsend goal v avfc 20131020 [640x360]",
        "caption": "Andros Townsend is mobbed after his cross inadvertently gave Spurs the lead at Aston Villa.",
        "type": "inline",
        "url": "http://espnfc.com/design05/images/2013/1020/spurscelebandrostownsendgoalvavfc20131020_640x360.jpg"
    }],
    "categories": [{
        "description": "Aston Villa",
        "type": "team",
        "sportId": 600,
        "teamId": 362,
        "team": {
            "id": 362,
            "description": "Aston Villa",
            "links": {
                "api": {
                    "teams": {
                        "href": "http://api.espn.com/v1/sports/soccer/teams/362"
                    }
                },
                "web": {
                    "teams": {
                        "href": "http://espnfc.com/team/_/id/362/aston-villa?ex_cid=espnapi_public"
                    }
                },
                "mobile": {
                    "teams": {
                        "href": "http://m.espn.go.com/soccer/clubhouse?teamId=362&ex_cid=espnapi_public"
                    }
                }
            }
        },
        "uid": "s:600"
    }, {
        "description": "Tottenham Hotspur",
        "type": "team",
        "sportId": 600,
        "teamId": 367,
        "team": {
            "id": 367,
            "description": "Tottenham Hotspur",
            "links": {
                "api": {
                    "teams": {
                        "href": "http://api.espn.com/v1/sports/soccer/teams/367"
                    }
                },
                "web": {
                    "teams": {
                        "href": "http://espnfc.com/team/_/id/367/tottenham-hotspur?ex_cid=espnapi_public"
                    }
                },
                "mobile": {
                    "teams": {
                        "href": "http://m.espn.go.com/soccer/clubhouse?teamId=367&ex_cid=espnapi_public"
                    }
                }
            }
        },
        "uid": "s:600"
    }],
    "published": "2013-10-20T17:12:43Z",
    "video": []
},
4

2 に答える 2

0

このコードでは、すでにほとんどのことを行っています。

$.each(data.headlines, function () {
    var h2 = $("<h2/>").text(this.headline);
    ol.append(h2)
});

thisheadlines配列内の現在のオブジェクトへの参照であり、プロパティに加えて、プロパティ (配列)headlineも持っています。imagesその配列を反復するには、別のネストされたループが必要です。

$.each(data.headlines, function () {
    var h2 = $("<h2/>").text(this.headline);
    ol.append(h2)
    $.each(this.images, function() {
        // do something with each of the images, using this to refer to it
        // that's a different this to the one before
    });
});
于 2013-10-21T13:50:20.717 に答える