我可以在 JSON 中提供 RSS 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/246577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:08:57  来源:igfitidea点击:

Can I serve RSS in JSON?

xmljsonrss

提问by Doug Miller

I am writing an RSS feed (for fun) and was looking at the spec here.

我正在写一个 RSS 提要(为了好玩)并且正在查看这里的规范。

RSS is a dialect of XML. All RSS files must conform to the XML 1.0 specification, as published on the World Wide Web Consortium (W3C) website.

RSS 是 XML 的一种方言。所有 RSS 文件都必须符合万维网联盟 (W3C) 网站上发布的 XML 1.0 规范。

Obviously this means that I am not serving 'pure' RSS if I choose the JSON option. That said, if I conform to the rest of the spec, is it likely that (customised) readers will be able to parse it?

显然,这意味着如果我选择 JSON 选项,我将不会提供“纯”RSS。也就是说,如果我符合规范的其余部分,(定制的)读者是否能够解析它?

To put it another way, if I conform to the spec, but using JSON instead of XML is it a usable RSS feed?

换句话说,如果我符合规范,但使用 JSON 而不是 XML 是可用的 RSS 提要吗?

editI am not sure I made myself clear. There is no XML involved. I want to write something akin to RSS (which is XML) using JSON instead. Obviously the readers of such a feed would need to be written to understand the JSON format. I was wondering if this had been done already. Are there services that serve a feed this way? Are there programs that can aggregate/understand this format. Is the RSS spec (sans the XML part) a useful spec to conform to in this case?

编辑我不确定我说清楚了。不涉及 XML。我想用 JSON 来写一些类似于 RSS(即 XML)的东西。显然,需要编写此类提要的读者才能理解 JSON 格式。我想知道这是否已经完成。是否有以这种方式提供提要的服务?是否有可以聚合/理解这种格式的程序。在这种情况下,RSS 规范(没有 XML 部分)是否是一个有用的规范?

rg

rg

{
"title":"example.com",
"link":"http://www.example.com/",
"description":"Awesome news about junk",
"items":[
    {
        "title":"An article",
        "link":"http://www.example.com/an-article",
        "descrition":"Some sample text here",
        "pubDate":"2008-10-27 11:06 EST",
        "author":"example author",
    },
    {
        "title":"Second",
        "link":"http://www.example.com/Seond",
        "descrition":"Some sample text here",
        "pubDate":"2008-10-25 23:20 EST",
        "author":"author mcauthor",
    },
    {
        "title":"third article",
        "link":"http://www.example.com/third-article",
        "descrition":"Some sample text here",
        "pubDate":"2008-10-25 23:18 EST",
        "author":"some other author",
    }
]
} 

采纳答案by jlbang

You're right that the client readingthe feed would have to have custom support for whatever the particulars of your JSON were. So you'd either need to make a custom feed reader to consume that information, or someone would have to propose a JSON feed standard, and it'd have to be widely adopted.

您是对的,读取提要的客户端必须对 JSON 的任何细节提供自定义支持。因此,您要么需要制作一个自定义提要阅读器来使用该信息,要么必须有人提出一个 JSON 提要标准,并且它必须被广泛采用。

Well, I think your desires have finally been met, friend!

好吧,我想你的愿望终于得到了满足,朋友!

Take a look at JSON Feed. As of this writing it's only about a week old, but it's already picking up steam, having been supported now by Feedly, Feedbin, News Explorer, NewsBlur, and more being added all the time.

看看JSON Feed。在撰写本文时,它只有大约一周的历史,但它已经得到了发展,现在得到了FeedlyFeedbinNews ExplorerNewsBlur 的支持,并且一直在添加更多内容。

If I had to pick a standard to use when generating a JSON version of RSS, I'd pick JSON Feed for sure.

如果我必须在生成 RSS 的 JSON 版本时选择要使用的标准,我肯定会选择 JSON Feed。

回答by Gareth

No, RSS is an XML-based format, and JSON is an different language rather than some kind of dialect. RSS readers won't understand JSON.

不,RSS 是一种基于 XML 的格式,而 JSON 是一种不同的语言,而不是某种方言。RSS 阅读器不会理解 JSON。

Your question is akin to asking 'Can I speak French in Chinese?'

你的问题类似于问“我可以用中文说法语吗?”

回答by Dan Esparza

I believe this has been done already.

我相信这已经完成了。

Take a look at this jQuery extension: jFeed - RSS/ATOM feed parser

看看这个 jQuery 扩展: jFeed - RSS/ATOM feed parser

jQuery.getFeed(options);

Options:

选项:

  • url:
  • data:
  • success:
  • 网址:
  • 数据:
  • 成功:

Example:

例子:

jQuery.getFeed({
       url: 'rss.xml',
       success: function(feed) {
           alert(feed.title);
       }
   });

Note that in this case, 'feed' would be a javascript object. If you wanted to pass this using JSON, you can just use the javascript JSON utility.

请注意,在这种情况下,'feed' 将是一个 javascript 对象。如果您想使用 JSON 传递它,您可以使用javascript JSON 实用程序

Example:

例子:

var myJSONText = JSON.stringify(feed);

回答by Kornel

Is the RSS spec (sans the XML part) a useful spec to conform to in this case?

在这种情况下,RSS 规范(没有 XML 部分)是否是一个有用的规范?

If you want to invent yet another syndication format, I recommend using Atom as a base. IMHO it has much cleaner, more consistent design, and has useful features like reliable updates of past items, makes distinction between summaries and full content, etc.

如果您想发明另一种联合格式,我建议使用 Atom 作为基础。恕我直言,它具有更清晰、更一致的设计,并具有有用的功能,例如对过去项目的可靠更新,区分摘要和完整内容等。

I was wondering if this had been done already.

我想知道这是否已经完成。

Flickr has JSON output format. They even have lolcode feed.

Flickr 有JSON 输出格式。他们甚至有lolcode feed

回答by James Newton-King

Json.NET - http://james.newtonking.com/projects/json-net.aspx- has support to convert any XML document to JSON.

Json.NET - http://james.newtonking.com/projects/json-net.aspx- 支持将任何 XML 文档转换为 JSON。

XmlDocument doc = new XmlDocument();

doc.LoadXml(@"<?xml version=""1.0"" standalone=""no""?>
<root>
  <person id=""1"">
    <name>Alan</name>
    <url>http://www.google.com</url>
  </person>
  <person id=""2"">
    <name>Louis</name>
    <url>http://www.yahoo.com</url>
  </person>
</root>");


string jsonText = JavaScriptConvert.SerializeXmlNode(doc);
//{
//  "?xml": {
//    "@version": "1.0",
//    "@standalone": "no"
//  },
//  "root": {
//    "person": [
//      {
//        "@id": "1",
//        "name": "Alan",
//        "url": "http://www.google.com"
//      },
//      {
//        "@id": "2",
//        "name": "Louis",
//        "url": "http://www.yahoo.com"
//      }
//    ]
//  }
//}

XmlDocument newDoc = (XmlDocument)JavaScriptConvert.DeerializeXmlNode(jsonText);

Assert.AreEqual(doc.InnerXml, newDoc.InnerXml);

回答by Robert Rossney

There are a bunch of different ways to serialize RSS into JSON. All of them work pretty much the same way: the elements and attributes become property names, the values become property values, etc. See Piyush Shah'stechnique, for example, which is a .NET implementation.

有很多不同的方法可以将 RSS 序列化为 JSON。它们的工作方式几乎相同:元素和属性变成属性名称,值变成属性值等等。例如,请参见Piyush Shah 的技术,这是一个 .NET 实现。

Transforming arbitrary XML to JSON using XSLT is simple enough that you can find a half-dozen examples of it on Google.

使用 XSLT 将任意 XML 转换为 JSON 非常简单,您可以在 Google 上找到六个示例。

As long as this is done consistently, JavaScript that can process an object model designed to replicate the data structure of the RSS specification should be able to handle the object model that the JSON deserializes into.

只要始终如一地执行此操作,可以处理旨在复制 RSS 规范数据结构的对象模型的 JavaScript 应该能够处理 JSON 反序列化为的对象模型。

Who are you planning to send this JSON to? That's the real question.

您打算将此 JSON 发送给谁?这才是真正的问题。

回答by Vihung

I know this is a fairly old question, and maybe irrelevant now.

我知道这是一个相当古老的问题,现在可能无关紧要。

However. I would suggest anyone looking to publish a RSS-like feed in JSON should use a new format that is rapidly gaining adoption; JSONFeed (https://jsonfeed.org).

然而。我建议任何希望在 JSON 中发布类似 RSS 的提要的人都应该使用一种正在迅速被采用的新格式;JSONFeed ( https://jsonfeed.org)。

回答by Uzbekjon

Well, if you are developing some javascript app you might want to get any RSS feeds as JSON to overcome cross-domain querying issue. There is a reliable Google provided solution that converts about any RSS to JSON. For jQuery lover's there is a universal RSS to JSON converter plugin.

好吧,如果您正在开发一些 javascript 应用程序,您可能希望将任何 RSS 提要作为 JSON 获取以克服跨域查询问题。有一个可靠的 Google 提供的解决方案,可以将任何 RSS 转换为 JSON。对于 jQuery 爱好者来说,有一个通用的 RSS 到 JSON 转换器插件

Example:

例子:

$.jGFeed('http://twitter.com/statuses/user_timeline/26767000.rss',
  function(feeds){

    // feeds is a javascript object with RSS content

  }, 10);

回答by Josh Habdas

Are there programs that can aggregate/understand this format.

是否有可以聚合/理解这种格式的程序。

You can use XSLT 3.0to transform XML to JSON and back to XML again. More info on how to accomplish the transforms to JSON here:

您可以使用XSLT 3.0将 XML 转换为 JSON,然后再转换回 XML。有关如何在此处完成转换为 JSON 的更多信息:

https://www.xml.com/articles/2017/02/14/why-you-should-be-using-xslt-30/.

https://www.xml.com/articles/2017/02/14/why-you-should-be-using-xslt-30/