XML 和 JSON——优点和缺点?

发布于 2024-10-31 05:36:41 字数 1696 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

西瑶 2024-11-07 05:36:42

JSON 的优点

  • 消息尺寸更小
  • 文档中有更多结构信息
    • 可以轻松区分数字 1 和字符串 "1",因为数字、字符串(和布尔值)在 JSON 中的表示方式不同。
    • 可以轻松区分单个项目和大小为 1 的集合(使用 JSON 数组)。
  • 更容易表示空值
  • 容易被 JavaScript 使用

XML 的优点

  • 命名空间允许共享标准结构
  • 更好的继承表示
  • 表达文档结构的标准方式:XML 模式、DTD 等
  • 解析标准: DOM、SAX、StAX
  • 查询标准:XQuery 和 XPath
  • 转换文档标准:XSLT

Draw

  • 人类可读
  • 易于解析

Advantages of JSON

  • Smaller message size
  • More structural information in the document
    • Can easily distinguish between the number 1 and 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).
  • Easier to represent a null value
  • Easily consumed by JavaScript

Advantages of 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

Draw

  • Human Readable
  • Easy to parse
北城孤痞 2024-11-07 05:36:42

XML

  • 可以有一个说明其格式的架构。
    • 质量控制人员对此感兴趣。您可以证明其格式符合预期,因此您可能不必像每次想要引用字段时那样热衷于检查其中是否存在字段。
    • (尽管这假设您不遗余力地根据其架构实际验证 XML。)
  • )每个字段的名称必须写两次。呃!

JSON

  • 不那么臃肿,更容易解析,并且可以说更易于人类阅读(如果你适当地间隔它)。
  • 不太强大:表达能力不足以将属性与值分开。

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!

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.
倾其所爱 2024-11-07 05:36:42

XML 的优点

  1. 多种语言和框架几乎无处不在的支持。很可能已经有一个工具可以帮助您从 XML 响应中提取信息。

  2. 如果您选择的话,它可以遵循具体的模式。一旦验证通过,您就可以说它是正确的并开始解析。

  3. 命名空间允许您划分 XML。

JSON 的优点

  1. 与 XML 相比轻量级。更少的字符 = 更少的时间通过网管

  2. 如果您需要 Web 应用程序的某些内容,则使用 Javascript 更容易处理。

Advantages of XML

  1. 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.

  2. It can adhere to a concrete schema if so you choose. Once it validates, you can say it's correct and start parsing.

  3. Namespaces allow you to divide the XML.

Advantages of JSON

  1. Lightweight in comparison to XML. Fewer characters = smaller time going through the internet tubes.

  2. Easier to handle with Javascript if you need something for a web application.

她比我温柔 2024-11-07 05:36:42

JSON - 更小,可以作为 JavaScript 对象本地加载(速度是一个值)

XML - 仍然是标准,但较旧(更慢,更大,但不仅仅是 JS)

JSON - smaller and can be natively loaded as JavaScript object (speed is a value)

XML - still standard, however older (slower, bigger, but not only JS)

糖果控 2024-11-07 05:36:42

JSON 是一种比 XML 简单得多的格式。 JSON 仅用于将数据描述为对象层次结构,而 XML 还具有许多其他功能。

如果您只需要发送简单的数据,那么 JSON 是一个不错的选择,因为这正是它的用途。

如果您需要一种更强大的方式来描述数据,那么您将需要 XML,因为 JSON 根本不做任何花哨的事情。

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.

If you only need to send plain simple data, then JSON is a good alternative, as that is just what it's intended for.

If you need a more powerful way of describing your data then you would need XML, as JSON simply doesn't do anything fancy.

你在我安 2024-11-07 05:36:42

它们是同样具有表现力的格式。 JSON 不太冗长。 JSON 可以很容易地在 JavaScript(和其他语言)中解析,XML 也可以很容易地被很多东西解析。

我认为最重要的考虑因素是什么将消耗数据 - 如果您已经有一堆 XML 解释代码,请坚持使用。如果您的所有客户都是 Web 开发人员,请使用 JSON。

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.

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.

甜嗑 2024-11-07 05:36:42

更简洁 - XML 使用的单词多于必要的

JSON 更快 - 解析 XML 软件缓慢且麻烦。由于解析大型 XML 文件的冗长性和成本,许多 DOM 操作库可能会导致您的应用程序使用大量内存。

JSON 数据模型的结构与数据相匹配:JSON 的数据结构是映射,而 XML 是树。尽管映射(只是键/值对)可能有限制,但这正是我们想要的,因为它更容易解释并且是可预测的。

在代码中:项目在代码中以相同的方式表示。在许多语言中,尤其是动态语言中,您只需“吞入 JSON”即可立即获得域对象。从 JSON 中的对象转换为代码中的对象很容易,因为它们是对齐的。当从 XML 中的对象转换为代码中的对象时,它们并不对齐,并且有很大的解释空间。

JSON 有限制,但这是一件好事:JSON 在可以建模的对象方面受到限制。有些人可能认为 XML 更好,因为可以对更多对象进行建模,并且它不会禁止开发人员。但即使 JSON 禁止开发人员,它也是以积极的方式,使代码更简单、更可预测且易于阅读。 XML 可以格式化为公司想要的任何外观和功能,但这使开发人员难以阅读、理解和转换。在大多数情况下,人们认为 XML 更好,因为开发人员可以做任何事情,但在简化的时代,少即是多,这使得 JSON 成为更好的替代方案。

Less verbose- XML uses more words than necessary

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 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.

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 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.

吹泡泡o 2024-11-07 05:36:41
  • JSON 更加紧凑,可以轻松地在 JavaScript 中加载。
  • XML 更严格并且支持模式和命名空间。

从表面上看,JSON 在各方面似乎都更优越 - 它灵活、更紧凑,并且在许多情况下更易于使用(特别是在使用 JavaScript 时),但它缺乏一些关键功能,特别是:

  • 模式支持

    即甲方指定文档格式的能力,以及乙方检查他们提供的内容是否与该格式匹配的能力。

    这在不同系统之间传递数据时至关重要,因为与预期格式的偏差可能意味着数据无法处理(或更糟糕的是,处理不正确)。

  • 命名空间支持

    即能够在同一文档中混合多个源读取(或多个源写入)的数据。

    一个实际的例子是 SOAP 协议 - 命名空间允许分离SOAP“信封”或“包装器”数据与序列化的应用程序数据一起传递。这允许 Web 框架处理和处理 SOAP 信封,然后将正文/有效负载数据传递到应用程序。

在开发需要快速、紧凑和方便的数据序列化的 Web 应用程序时,JSON 非常有用,但它的灵活性使其不像 XML 那样适合在不同系统之间传输数据或存储将要读取的数据由第 3 方。

也许这些功能最终会出现在 JSON 中,但目前 XML 是 Web 服务和文件格式等内容的主要格式。

  • JSON is more compact and can be easily loaded in JavaScript.
  • XML is stricter and has support for schemas and namespaces.

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:

  • 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.

    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 SOAP protocol - 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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文