解析JSON以获得谷歌新闻提要中描述的干净、纯文本

Parse JSON for clean, pure text of the description in Google News feed

本文关键字:描述 文本 JSON 谷歌 新闻 解析      更新时间:2023-09-26

我想检索在Google News RSS提要上找到的故事的description的纯文本(没有任何HTML),该RSS提要已使用Google AJAX API进行处理,以将RSS转换为JSON 我如何解析"JSON’d"提要以获得干净的description/content文本,如下面的第二张屏幕截图所示

屏幕截图:
原始rss:https://i.stack.imgur.com/oQYmz.jpg
漂亮,干净的描述通过Y!管道:https://i.stack.imgur.com/mGCSU.jpg

JSFiddle:http://jsfiddle.net/KuLQw/1/
可以在description/content上使用$.text(),但如何在最后删除额外的相关垃圾(参见下面对答案的评论)

$.getJSON('http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=100&callback=?&q=http%3A%2F%2Fnews.google.com%2F%3Foutput%3Drss', function(data){
    $.each(data.responseData.feed.entries, function(i, item){
    // variables
    var title = item.title;
    var url = item.link;
    var pub_date = item.pubDate;
    var description = item.content;
    // rendering
    $('div.news').append('<div class="news-story"><p><a href="'+url+'">'+title+'</a></p><div style="border: 1px solid red">'+$(item.content).text()+'</div></div>');
    });
});​

这是一个开始:

var url='http://pipes.yahoo.com/pipes/pipe.run?_id=2FV68p9G3BGVbc7IdLq02Q&_render=json&feedcount=10&feedurl=http%3A%2F%2Fnews.google.com%2F%3Foutput%3Drss';
$.getJSON(url+'&_callback=?',function(x){
    $.each(x.value.items,function(k,v){
        $('#out').append('<div>'+v.title+'</div>');
    });
});

演示:http://jsfiddle.net/h4H4p/

JSON不是Javascript,所以访问JSON数据就像访问一个简单的Javascript对象一样。所以你的描述在NAME_OF_YOUR_JSON_VAR.value.description