为什么JSON比XML更轻量级? [英] Why is JSON more lightweight than XML?

查看:420
本文介绍了为什么JSON比XML更轻量级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现JSON和XML之间的区别.同样,两者都是用于系统之间的数据交换,但是JSON和XML之间存在很大的区别,即JSON比XML更轻便.但是我找不到JSON轻量级的实际原因.是什么使JSON轻量级?

我发现的一个答案是,在JSON中没有很多额外的xml标记.实际是什么意思.还有其他一些原因来说明JSON为什么是轻量级的吗?

解决方案

您是否已看过 JSON和XML文档的示例?

尽管JSON和XML都用来表示任意的,但XML却更加冗长.树中的每个元素都有一个名称(元素类型名称),并且该元素必须包含在一对匹配的标签中.相比之下,JSON用类似于Javascript的嵌套数组"表示法来表示树(实际上,JSON文档可以从字面上解析为Javascript以产生相应的数组).

区别.

XML在语义上当然更丰富,因为元素可以用任意数量的属性装饰,并且元素可以包含自由文本流的混合内容,并且进一步嵌套元素.例如,以下代码片段很难用JSON表示:

<paragraph>
   This is an entire paragraph of <emph>text</emph>. It is interspersed with
   further, nested <emph>XML elements</emph>, as well as the occasional
   <link href="http://stackoverflow.com/a/12346394/596781">hyperlink</link>.
</paragraph>

另一方面,类似 data 的文档在JSON中可能要简单得多.想象一下:

<properties>
    <property type="int" name="ID">123</property>
    <property type="date" name="birthday">19700101</property>
    <references>
        <url>http://stackoverflow.com/a/12346394/596781</url>
        <doi>something.made.up</doi>
    </references>
</properties>

这将成为一个非常紧凑的JSON数组.

次要考虑的是围绕这两种格式的工具集的数量.尽管JSON工具主要用于解析JSON数据,但是W3C一直在开发大量的粘附技术,以系统方式处理XML,例如XSLT和XPATH.

简而言之,我个人认为XML是关于文档的,而JSON是关于数据的.对于大型结构化文档,XML会显得更加自然和有用,而JSON通常是用于传输数据集的更简单,更省时间的格式.

I was finding the difference between JSON and XML. As, both are for data interchange between systems, but there is a big difference between JSON and XML that JSON is more lightweight than XML. But I am unable to find the actual reason that why the JSON is light-weight. What makes JSON light-weight??

One answer I found is that in JSON there is not a lot of extra xml markup. What does it actual mean. Are there some more reasons that describes why JSON is lightweight?

解决方案

Have you looked at examples of JSON and XML documents?

While both JSON and XML are used to represent arbitrary trees of data, XML is far more verbose. Every element in the tree has a name (the element type name), and the element must be enclosed in a matching pair of tags. By contrast, JSON expresses trees in a "nested array" type of notation akin to that of Javascript (in fact, a JSON document can literally be parsed as Javascript to result in the corresponding array).

Take a look at the differences.

XML is of course semantically richer, since elements can be decorated with an arbitrary number of attributes, and elements may contain mixed content of free text flow and further, nested elements. For example, the following snippet would be tedious to express as JSON:

<paragraph>
   This is an entire paragraph of <emph>text</emph>. It is interspersed with
   further, nested <emph>XML elements</emph>, as well as the occasional
   <link href="http://stackoverflow.com/a/12346394/596781">hyperlink</link>.
</paragraph>

On the other hand, data-like documents can be much simpler in JSON. Imagine this:

<properties>
    <property type="int" name="ID">123</property>
    <property type="date" name="birthday">19700101</property>
    <references>
        <url>http://stackoverflow.com/a/12346394/596781</url>
        <doi>something.made.up</doi>
    </references>
</properties>

This becomes a very compact JSON array.

A secondary consideration is the amount of toolsets that surround both formats. While JSON tools are mainly about parsing JSON data, the W3C has been developing a very large set of adherent technologies to manipulate XML in a systematic fashion, such as XSLT and XPATH.

In a nutshell, and as a very personal opinion, I'd say that XML is about the document and JSON is about data. XML will feel more natural and useful for large, structured documents, while JSON is often the simpler and less verbose format for transmitting data sets.

这篇关于为什么JSON比XML更轻量级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆