2017-06-22 30 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; 
};