2013-10-21 53 views
0

我目前正在使用API​​並需要訪問每個故事的標題,圖片和移動鏈接,我已成功地獲得了標題和圖片,但獲取移動鏈接一直很困難。JSON解析中的鏈接問題

.done(function(data) {  
    var ol = $("<ol/>"); 

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

    ol.append(h2) 

    $.each(this.images, function(){ 
     var img = $("<img>").attr("src", this.url); 
     ol.append(img)  
    }); 
    }); 
    $("body").append(ol); 
}); 

檢查查詢API後的反應體示出了移動鏈路的語法是從該標題和圖像的不同。如何訪問此鏈接並在瀏覽器中顯示此鏈接而不顯示實際鏈接?這就是響應主體的樣子;

{ 
"timestamp": "2013-10-21T14:50:18Z", 
"resultsOffset": 0, 
"status": "success", 
"resultsLimit": 10, 
"resultsCount": 27, 
"headlines": [{ 
    "headline": "Portugal land Sweden in playoff draw", 
    "keywords": ["UEFA WCQ: Portugal land Sweden in playoff draw"], 
    "lastModified": "2013-10-21T14:17:13Z", 
    "audio": [], 
    "premium": false, 
    "mobileStory": "", 
    "links": { 
     "api": { 
      "news": { 
       "href": "http://api.espn.com/v1/sports/news/1588808?region=GB" 
      } 
     }, 
     "web": { 
      "href": "http://espnfc.com/news/story/_/id/1588808/portugal-land-sweden-playoff-draw?ex_cid=espnapi_public" 
     }, 
     "mobile": { 
      "href": "http://m.espn.go.com/soccer/story?storyId=1588808&ex_cid=espnapi_public" 
     } 
    }, 
    "type": "Story", 
    "related": [], 
    "id": 1588808, 
    "story": "", 
    "title": "Portugal land Sweden in playoff draw", 
    "linkText": "UEFA WCQ: Portugal land Sweden in playoff draw", 
    "byline": "ESPN staff", 
    "description": "European qualifying", 
    "images": [{ 
     "height": 155, 
     "alt": "World Cup qualifiers", 
     "width": 275, 
     "name": "Zlatan Ibrahimivoc Sweden Cristiano Ronaldo [275x155]", 
     "caption": "One of either Zlatan Ibrahimovic or Cristiano Ronaldo will not be going to Brazil.", 
     "type": "inline", 
     "url": "http://espnfc.com/design05/images/2013/1021/zlatanibrahimivocswedencristianoronaldo_275x155.jpg" 
    }], 
+0

'data.headlines [I] .links.mobile' ? –

回答

1

這使得你的h2鏈接到網頁版:

.done(function(data){ 
    var ol = $("<ol/>"); 

    $.each(data.headlines, function() { 
     var link = $("<a/>", { 
      'href': this.links.web.href 
     }).append(this.headline); 

     var h2 = $("<h2/>").append(link); 

     ol.append(h2) 

     $.each(this.images, function(){ 
      var img = $("<img>").attr("src", this.url); 
      ol.append(img) 
     }); 
    }); 
    $("body").append(ol); 
}); 
+0

不幸的是,我使用你的方法得到了和我一樣的結果,就像我之前說過的,獲取標題和圖像沒有問題,獲得鏈接到故事就是問題所在。 – user2902458

+0

您應該可以通過'this.links.web.href'或類似的方式獲取鏈接...您從哪個URL獲取JSON數據? (我想拉飼料和測試) – keithhatfield

+0

這是網址,你將需要一個API密鑰來訪問它雖然[http://api.espn.com/v1/sports/soccer/news/headlines/top/ ?apikey = ******************&callback =?] – user2902458

0

我認爲這應該工作:

.done(function (data) { 
    var ol = $("<ol/>"); 

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

     ol.append(h2); 

     $('<a>Click to view article</a>', { 
      href = this.links.mobile 
     }).appendTo(ol); 

     $.each(this.images, function() { 
      var img = $("<img>").attr("src", this.url); 
      ol.append(img) 
     }); 
    }); 
    $("body").append(ol); 
});