对于 REST API,CSV、JSON 和 XML 的相对优点是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/138929/
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:01  来源:igfitidea点击:

What are the relative merits of CSV, JSON and XML for a REST API?

xmljsonapirestcsv

提问by Mike McQuaid

We're currently planning a new APIfor an application and debating the various data formats we should use for interchange. There's a fairly intense discussion going on about the relative merits of CSV, JSONand XML.

我们目前正在计划一个新API的应用程序,并讨论我们应该用于交换的各种数据格式。关于CSV,JSON和的相对优点正在进行相当激烈的讨论XML

Basically, the crux of the argument is whether we should support CSVat all because of the lack of recursion (i.e. having a document which has multiple authorsand multiple referenceswould require multiple API callsto obtain all the information).

基本上,争论的关键是我们是否应该支持CSV,因为缺乏递归(即有一个文档,multiple authors并且multiple references需要multiple API calls获取所有的information)。

In the experiences you may have had when working with information from Web APIsand things we can do to make the lives easier for the developers working with our API.

在经验与信息的工作时,你可能已经Web APIs和事情可以做,使生活与我们的合作开发商更容易API



Our decision:

我们的决定:

We've decided to provide XMLand JSONdue to the difficulty in recursion in CSVneeding multiple calls for a single logical operation. JSONdoesn't have a parser in Qtand Protocol Buffersdoesn't seem to have a non-alpha PHPimplementation so they are out for the moment too but will probably be supported eventually.

我们决定提供XML并且JSON由于递归CSV需要多次调用单个逻辑操作的困难。JSON没有一个解析器QtProtocol Buffers似乎并不具有non-alpha PHP实施,使他们走出暂时太多,但可能会被最终支持。

采纳答案by Allain Lalonde

CSV is right out. JSON is a more compact object notation than XML, so if you're looking for high volumes it has the advantage. XML has wider market penetration (I love that phrase) and is supported by all programming languages and their core frameworks. JSON is getting there (if not already there).

CSV 是正确的。JSON 是一种比 XML 更紧凑的对象表示法,因此如果您正在寻找高容量,它具有优势。XML 具有更广泛的市场渗透率(我喜欢这句话),并且得到所有编程语言及其核心框架的支持。JSON 正在到达那里(如果还没有)。

Personally, I like the brackets. I would bet more devs are comfortable with working with xml data than with json.

就个人而言,我喜欢括号。我敢打赌,与使用 json 相比,更多的开发人员更愿意使用 xml 数据。

回答by Allain Lalonde

Advantages:

好处:

  • XML - Lots of libraries, Devs are familiar with it, XSLT, Can be easiily Validated by both client and server (XSD, DTD), Hierarchical Data
  • JSON - easily interpreted on client side, compact notation, Hierarchical Data
  • CSV - Opens in Excel(?)
  • XML - 许多库,开发人员熟悉它,XSLT,可以由客户端和服务器轻松验证(XSD,DTD),分层数据
  • JSON - 易于在客户端解释,紧凑的符号,分层数据
  • CSV - 在 Excel 中打开(?)

Disadvantages:

缺点:

  • XML - Bloated, harder to interpret in JavaScript than JSON
  • JSON - If used improperly can pose a security hole (don't use eval), Not all languages have libraries to interpret it.
  • CSV - Does not support hierarchical data, you'd be the only one doing it, it's actually much harder than most devs think to parse valid csv files (CSV values can contain new lines as long as they are between quotes, etc).
  • XML - 臃肿,在 JavaScript 中比 JSON 更难解释
  • JSON - 如果使用不当会造成安全漏洞(不要使用 eval),并非所有语言都有解释它的库。
  • CSV - 不支持分层数据,您将是唯一一个这样做的人,它实际上比大多数开发人员认为解析有效的 csv 文件要困难得多(CSV 值可以包含新行,只要它们在引号之间,等等)。

Given the above, I wouldn't even bother supporting CSV. The client can generate it from either XML or JSON if it's really needed.

鉴于上述情况,我什至不会费心支持 CSV。如果确实需要,客户端可以从 XML 或 JSON 生成它。

回答by JeeBee

CSV has so many problems as a complex data model that I wouldn't use it. XML is very flexible and easy to program with - clients will have no problem coding XML generators and parsers, you can even provide sample parsers using SAX.

CSV 作为一个复杂的数据模型有很多问题,我不会使用它。XML 非常灵活且易于编程 - 客户端在编写 XML 生成器和解析器时没有问题,您甚至可以使用 SAX 提供示例解析器。

Have you checked out Google's network data format? It's called Protocol Buffers. Don't know if it is useful for a REST service however as it skips that whole HTTP layer too.

你查过谷歌的网络数据格式了吗?它被称为协议缓冲区。不知道它是否对 REST 服务有用,因为它也跳过了整个 HTTP 层。

回答by William Keller

XML can be a bit heavyweight at times. JSON is quite nice, though, has good language support, and JSON data can be translated directly to native objects on many playforms.

XML 有时可能有点重量级。JSON 非常好,但具有良好的语言支持,并且 JSON 数据可以在许多游戏形式上直接转换为本地对象。

回答by Harald Scheirich

I don't have any experience with JSON, CSV works up to a point when your data is very tabular and evenly structured. XML can become unwieldy very quickly, especially if you don't have a tool that creates the bindings to your objects automatically.

我对 JSON 没有任何经验,当您的数据非常表格且结构均匀时,CSV 可以工作。XML 很快就会变得笨拙,特别是如果您没有自动创建对象绑定的工具。

I have not tried this either but Google's Protocol Bufferslook really good, simple format, creates automatic bindings to C++, Java and Python and implements serialisation and deserialisation of the created objects.

我也没有尝试过,但Google 的 Protocol Buffers看起来非常好,格式简单,创建了到 C++、Java 和 Python 的自动绑定,并实现了创建对象的序列化和反序列化。

回答by Haroldo_OK

Asides from what Allain Lalonde already said, one additional advantage of CSV is that it tends to be more compact than XML or even JSON. So, if your data is strictlytabular, with a completely flat hyerarchy, CSV may be a correct choice. Additonal disadvantages of CSV is that it may use different delimiters and decimal separators, depeding on which tool (and even country!) generated it.

除了 Alllain Lalonde 已经说过的,CSV 的另一个优点是它往往比 XML 甚至 JSON 更紧凑。因此,如果您的数据是严格的表格形式,并且具有完全扁平的层次结构,那么 CSV 可能是正确的选择。CSV 的其他缺点是它可能使用不同的分隔符和小数点分隔符,具体取决于生成它的工具(甚至国家/地区!)。