XML 和 JSON —— 优点和缺点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5615352/
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
XML and JSON -- Advantages and Disadvantages?
提问by user541686
I recently heard about JavaScript Object Notation (JSON), and after looking it up, it seems like it's becoming rather popular as an alternative to the Extensible Markup Language (XML).
我最近听说了 JavaScript Object Notation (JSON),查了一下之后,它似乎作为可扩展标记语言 (XML) 的替代品变得相当流行。
I went on thispage for more info, but it seemed more of an XML-bashing page rather than a comparison page. So I thought I should ask here:
我访问了这个页面以获取更多信息,但它似乎更像是一个 XML bashing 页面,而不是一个比较页面。所以我想我应该在这里问:
What are the benefits of JSON as compared to XML, and why (if at all) should we choose one over the other?
与 XML 相比,JSON 有什么好处,为什么(如果有的话)我们应该选择一个而不是另一个?
回答by Justin
- JSON is more compact and can be easily loaded in JavaScript.
- XML is stricter and has support for schemas and namespaces.
- JSON 更紧凑,可以轻松地在 JavaScript 中加载。
- XML 更严格并且支持模式和命名空间。
On the face of it JSON seems superior in every way - it's flexible, more compact and in many cases easier to use (especially when working with JavaScript), however it lacks some key features, in particular:
从表面上看,JSON 似乎在各方面都更胜一筹——它灵活、更紧凑,并且在许多情况下更易于使用(尤其是在使用 JavaScript 时),但它缺乏一些关键特性,特别是:
- Schema support,
- 模式支持,
I.e. the ability for party A to specify the format of a document, and the ability for party B to check that they are supplying something that matches this format.
即,A 方指定文档格式的能力,以及 B 方检查他们是否提供与此格式匹配的内容的能力。
This is crucial when passing data between separate systems, where a deviation from the expected format might mean that the data cannot be processed (or worse, is processed incorrectly).
这在不同系统之间传递数据时至关重要,在这种情况下,与预期格式的偏差可能意味着无法处理数据(或者更糟的是,处理不正确)。
- Namespace support,
- 命名空间支持,
I.e. the ability to mix data intended to be read by multiple sources (or written by multiple sources) in the same document.
即在同一文档中混合旨在由多个源读取(或由多个源写入)的数据的能力。
An example of this in action is the SOAPprotocol - namespaces allow for the separation of the SOAP "Envelope", or "Wrapper" data which is passed alongside the serialised application data. This allows web frameworks process and handle the SOAP Envelope and then pass the body / payload data onto the application.
这方面的一个例子是SOAP协议 - 名称空间允许分离 SOAP“信封”或“包装器”数据,这些数据与序列化的应用程序数据一起传递。这允许 Web 框架处理和处理 SOAP Envelope,然后将正文/有效负载数据传递给应用程序。
JSON is very useful when developing a web application where fast, compact and convenient serialisation of data is required, however it's flexible nature is the very thing that makes it less suitable than XML for transferring data between separate systems, or storing data that will be read by 3rd parties.
在开发需要快速、紧凑和方便的数据序列化的 Web 应用程序时,JSON 非常有用,但是它的灵活特性使其不如 XML 适合在不同系统之间传输数据或存储将要读取的数据由第 3 方。
Perhaps in time these sorts of features will appear in JSON, but for now XML is the dominant format for things like web services and file formats.
也许随着时间的推移,这些类型的特性会出现在 JSON 中,但就目前而言,XML 是 Web 服务和文件格式等事物的主要格式。
回答by bdoughan
Advantages of JSON
JSON 的优点
- Smaller message size
- More structural information in the document
- Can easily distinguish between the number
1and the string"1"as numbers, strings (and Booleans) are represented differently in JSON. - Can easily distinguish between single items and collections of size one (using JSON arrays).
- Can easily distinguish between the number
- Easier to represent a null value
- Easily consumed by JavaScript
- 较小的消息大小
- 文档中的更多结构信息
- 可以轻松区分数字
1和字符串,"1"因为数字、字符串(和布尔值)在 JSON 中的表示方式不同。 - 可以轻松区分单个项目和大小为 1 的集合(使用 JSON 数组)。
- 可以轻松区分数字
- 更容易表示空值
- 很容易被 JavaScript 使用
Advantages of XML
XML 的优点
- Namespaces allow for sharing of standard structures
- Better representation for inheritance
- Standard ways of expressing the structure of the document: XML schema, DTD, etc
- Parsing standards: DOM, SAX, StAX
- Standards for querying: XQuery and XPath
- Standards for transforming a document: XSLT
- 命名空间允许共享标准结构
- 更好的继承表示
- 表示文档结构的标准方式:XML 模式、DTD 等
- 解析标准:DOM、SAX、StAX
- 查询标准:XQuery 和 XPath
- 转换文档的标准:XSLT
Draw
画
- Human Readable
- Easy to parse
- 人类可读
- 易于解析
回答by Lightness Races in Orbit
XML
XML
- Can have a schema that states its format.
- This is of interest to quality control people. You can prove that its format matches what is expected, and therefore you may not have to be quite as fervent as you might otherwise be at checking that a field exists within it every time you want to reference one.
- (Though this pre-supposes that you go out of your way to actually validate the XML against its schema.)
- Bloated; each field name has to be written out twice per field. Ew!
- 可以有一个说明其格式的模式。
- 这是质量控制人员的兴趣所在。您可以证明其格式与预期相符,因此您可能不必像在每次要引用某个字段时检查其中是否存在某个字段那样热心。
- (尽管这预先假设您不遗余力地根据其架构实际验证 XML。)
- 臃肿;每个字段名称必须在每个字段中写出两次。诶!
JSON
JSON
- Far less bloated, easier to parse and arguably more human readable (if you space it out properly).
- Not quite as powerful: not expressive enough to separate attributes from values.
- 不那么臃肿,更容易解析并且可以说更具人类可读性(如果你正确地隔开它)。
- 不那么强大:表达能力不足以将属性与值分开。
回答by Only Bolivian Here
Advantages of XML
XML 的优点
Near ubiquitous support in a wide array of languages and frameworks. More likely than not there's already a tool out there to help your extract information from an XML response.
It can adhere to a concrete schema if so you choose. Once it validates, you can say it's correct and start parsing.
Namespaces allow you to divide the XML.
在各种语言和框架中几乎无处不在的支持。很可能已经有一个工具可以帮助您从 XML 响应中提取信息。
如果您愿意,它可以遵循具体的模式。一旦验证通过,您就可以说它是正确的并开始解析。
命名空间允许您划分 XML。
Advantages of JSON
JSON 的优点
Lightweight in comparison to XML. Fewer characters = smaller time going through the
internet tubes.Easier to handle with Javascript if you need something for a web application.
与 XML 相比轻量级。更少的字符 = 通过
internet tubes.如果您需要 Web 应用程序的某些内容,则使用 Javascript 更容易处理。
回答by bensiu
JSON - smaller and can be natively loaded as JavaScript object (speed is a value)
JSON - 更小,可以作为 JavaScript 对象本地加载(速度是一个值)
XML - still standard, however older (slower, bigger, but not only JS)
XML - 仍然是标准的,但更旧(更慢,更大,但不仅仅是 JS)
回答by Guffa
JSON is a much simpler format than XML. JSON is intended only for describing data as an object hierarchy, while XML has many other features as well.
JSON 是一种比 XML 简单得多的格式。JSON 仅用于将数据描述为对象层次结构,而 XML 也具有许多其他功能。
If you only need to send plain simple data, then JSON is a good alternative, as that is just what it's intended for.
如果您只需要发送简单的数据,那么 JSON 是一个不错的选择,因为这正是它的用途。
If you need a more powerful way of describing your data then you would need XML, as JSON simply doesn't do anything fancy.
如果您需要一种更强大的方式来描述您的数据,那么您将需要 XML,因为 JSON 根本不会做任何花哨的事情。
回答by Riley Lark
They're equally expressive formats. JSON is less verbose. JSON can be parsed easily in JavaScript (and other languages), and XML is parsed easily by many things too.
它们是同样具有表现力的格式。JSON 不那么冗长。JSON 可以在 JavaScript(和其他语言)中轻松解析,XML 也可以通过许多东西轻松解析。
I think the most important consideration is what will be consuming the data - if you already have a bunch of XML-interpreting code, stick with that. If all your clients are web developers, go with JSON.
我认为最重要的考虑是什么将消耗数据 - 如果您已经有一堆 XML 解释代码,请坚持使用。如果您的所有客户都是 Web 开发人员,请使用 JSON。
回答by vks
Less verbose- XML uses more words than necessary
不那么冗长——XML 使用的单词比必要的多
JSON is faster- Parsing XML software is slow and cumbersome. Many of these DOM manipulation libraries can lead to your applications using large amounts of memory due to the verbosity and cost of parsing large XML files.
JSON 更快——解析 XML 软件既慢又麻烦。由于解析大型 XML 文件的冗长和成本,许多这些 DOM 操作库可能导致您的应用程序使用大量内存。
JSON data model's structure matches the data: JSON's data structure is a map whereas XML is a tree. Although a map (just key/value pairs) can be limiting, that's what we want, because it is easier to interpret and is predictable.
JSON 数据模型的结构与数据匹配:JSON 的数据结构是地图,而 XML 是树。尽管映射(仅键/值对)可能会受到限制,但这正是我们想要的,因为它更易于解释且可预测。
In code: Items are represented the same way in code. In many languages, especially dynamic ones, you can just ‘slurp in the JSON' and you immediately have your domain object. It is easy to go from objects in JSON to the objects in code because they align. When going from objects in XML to objects in code they do not align and there is a lot of room for interpretation.
在代码中:项目在代码中以相同的方式表示。在许多语言中,尤其是动态语言中,您只需“在 JSON 中啜饮”即可立即拥有域对象。从 JSON 中的对象到代码中的对象很容易,因为它们是对齐的。当从 XML 中的对象到代码中的对象时,它们不会对齐,并且有很大的解释空间。
JSON is limiting, but that's a good thing: JSON is limited in terms of what objects can be modeled. Some may think XML is better because more objects can be modeled and it doesn't prohibit developers. But even though JSON prohibits developers, it is in a positive way, making the code simpler, more predictable, and easy to read. XML can be formatted to look and function any way a company wants, but it makes it difficult for developers to read, understand, and convert. In most cases people believe XML is better because developers can do anything under the sun but in the age of simplifying, less is more, making JSON a better alternative.
JSON 是有限制的,但这是一件好事:就可以建模的对象而言,JSON 是有限的。有些人可能认为 XML 更好,因为可以建模更多的对象,而且它不会禁止开发人员。但即使 JSON 禁止开发人员,它也是一种积极的方式,使代码更简单、更可预测且易于阅读。XML 可以格式化为公司想要的任何外观和功能,但它使开发人员难以阅读、理解和转换。在大多数情况下,人们认为 XML 更好,因为开发人员可以在阳光下做任何事情,但在简化的时代,少即是多,使 JSON 成为更好的选择。

