2017-06-22 32 views
-1

基於以下模式,如何創建一個變量從json中獲取標籤?博主JSON - 如何獲取類別/標籤?

var postLink = function(entry) { 
    for (var j = 0; j < entry.link.length; j++) { 
     if (entry.link[j].rel == 'alternate') { 
      var postURL = entry.link[j].href; 
      return postURL; 
      break; 
     } 
    } 
}; 

var postTitle = function(entry) { 
    var postTitle = entry.title.$t; 
    return postTitle; 
}; 

function Full(json) { 
    for (var i = 0; i < json.feed.entry.length; i++) { 
     var entry = json.feed.entry[i]; 
     var item1 = '<h3><a href="' + postLink(entry) + '">' + postTitle(entry) + '</a></h3>'; 
     document.write(item1); 
    } 
} 

這段代碼打印結果:

<script src="/feeds/posts/summary/-/LABEL?alt=json-in-script&amp;callback=Full&amp;max-results=2"></script> 

結果是文章標題和鏈接只是......我只是想添加變量代碼標籤,以及...

我想裏面的代碼:

var postLabel = function(entry) { 
     //some code here 
}; 

...和內部var item1我會補充一點:+ postLabel(entry) +

我需要遵循postLink和postTitle的模式,但它超出了我的能力......感謝您的幫助!

+0

你......忘了包括json以及你如何調用'Full'。這是jsonp嗎? –

+1

我確實要求澄清。你還沒有提供任何。 –

+0

我添加了一些更多的細節:) – willr

回答

0

經過幾個小時的反覆試驗,我找到了一個解決方案,並以其他類似代碼爲基礎!我只知道css,我開始喜歡js :)

var postLabel = function(entry) { 
    var postLabel = entry.category[0].term; 
    return postLabel; 
};