json 和 xml 有什么区别

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

what is the difference between json and xml

xmljsoncomparison

提问by pia

What is the difference between JSON and XML?

JSON 和 XML 有什么区别?

回答by Daniel Earwicker

The fundamental difference, which no other answer seems to have mentioned, is that XML is a markup language (as it actually says in its name), whereas JSON is a way of representing objects (as also noted in its name).

似乎没有其他答案提到的根本区别在于,XML 是一种标记语言(正如它的名称所言),而 JSON 是一种表示对象的方式(正如其名称所指出的那样)。

A markup language is a way of adding extra information to free-flowing plain text, e.g

标记语言是一种向自由流动的纯文本添加额外信息的方法,例如

Here is some text.

With XML (using a certain element vocabulary) you can put:

使用 XML(使用特定的元素词汇),您可以输入:

<Document>
    <Paragraph Align="Center">
        Here <Bold>is</Bold> some text.
    </Paragraph>
</Document>

This is what makes markup languages so useful for representing documents.

这就是标记语言对于表示文档如此有用的原因。

An object notation like JSON is not as flexible. But this is usually a good thing. When you're representing objects, you simply don't need the extra flexibility. To represent the above example in JSON, you'd actually have to solve some problems manually that XML solves for you.

像 JSON 这样的对象表示法没有那么灵活。但这通常是一件好事。当您表示对象时,您根本不需要额外的灵活性。要在 JSON 中表示上述示例,您实际上必须手动解决一些 XML 为您解决的问题。

{
    "Paragraphs": [
        {
            "align": "center",
            "content": [
                "Here ", {
                    "style" : "bold",
                    "content": [ "is" ]
                },
                " some text."
            ]
        }
    ]
}

It's not as nice as the XML, and the reason is that we're trying to do markup with an object notation. So we have to invent a way to scatter snippets of plain text around our objects, using "content" arrays that can hold a mixture of strings and nested objects.

它不如 XML 好,原因是我们试图用对象表示法进行标记。因此,我们必须发明一种方法来将纯文本片段散布在我们的对象周围,使用可以包含字符串和嵌套对象的混合的“内容”数组。

On the other hand, if you have typical a hierarchy of objects and you want to represent them in a stream, JSON is better suited to this task than HTML.

另一方面,如果您有一个典型的对象层次结构,并且想要在流中表示它们,那么 JSON 比 HTML 更适合此任务。

{
    "firstName": "Homer",
    "lastName": "Simpson",
    "relatives": [ "Grandpa", "Marge", "The Boy", "Lisa", "I think that's all of them" ]
} 

Here's the logically equivalent XML:

这是逻辑上等效的 XML:

<Person>
    <FirstName>Homer</FirstName>
    <LastName>Simpsons</LastName>
    <Relatives>
        <Relative>Grandpa</Relative>
        <Relative>Marge</Relative>
        <Relative>The Boy</Relative>
        <Relative>Lisa</Relative>
        <Relative>I think that's all of them</Relative>
    </Relatives>
</Person>

JSON looks more like the data structures we declare in programming languages. Also it has less redundant repetition of names.

JSON 看起来更像是我们在编程语言中声明的数据结构。此外,它的名称冗余重复较少。

But most importantly of all, it has a defined way of distinguishing between a "record" (items unordered, identified by names) and a "list" (items ordered, identified by position). An object notation is practically useless without such a distinction. And XML has no such distinction! In my XML example <Person>is a record and <Relatives>is a list, but they are not identified as such by the syntax.

但最重要的是,它有一种定义的方式来区分“记录”(无序的项目,由名称标识)和“列表”(已排序的项目,由位置标识)。如果没有这种区别,对象符号实际上是无用的。而 XML 没有这样的区别!在我的 XML 示例中,<Person>是一个记录并且<Relatives>是一个列表,但语法并未将它们标识为这样。

Instead, XML has "elements" versus "attributes". This looks like the same kind of distinction, but it's not, because attributes can only have string values. They cannot be nested objects. So I couldn't have applied this idea to <Person>, because I shouldn't have to turn <Relatives>into a single string.

相反,XML 具有“元素”与“属性”。这看起来是同一种区别,其实不然,因为属性只能有字符串值。它们不能是嵌套对象。所以我不能把这个想法应用到<Person>,因为我不应该<Relatives>变成一个单一的字符串。

By using an external schema, or extra user-defined attributes, you can formalise a distinction between lists and records in XML. The advantage of JSON is that the low-level syntax has that distinction built into it, so it's very succinct and universal. This means that JSON is more "self describing" by default, which is an important goal of both formats.

通过使用外部模式或额外的用户定义属性,您可以在 XML 中形式化列表和记录之间的区别。JSON 的优点是低级语法内置了这种区别,因此非常简洁和通用。这意味着默认情况下 JSON 更“自我描述”,这是两种格式的一个重要目标。

So JSON should be the first choice for object notation, where XML's sweet spot is document markup.

因此 JSON 应该是对象表示法的首选,其中 XML 的最佳点是文档标记。

Unfortunately for XML, we already have HTML as the world's number one rich text markup language. An attempt was made to reformulate HTML in terms of XML, but there isn't much advantage in this.

不幸的是,对于 XML,我们已经将 HTML 作为世界第一的富文本标记语言。曾尝试根据 XML 重新格式化 HTML,但这并没有太大的优势。

So XML should (in my opinion) have been a pretty limited niche technology, best suited only for inventing your own rich text markup languages if you don't want to use HTML for some reason. The problem was that in 1998 there was still a lot of hype about the Web, and XML became popular due to its superficial resemblance to HTML. It was a strange design choice to try to apply to hierarchical data a syntax actually designed for convenient markup.

因此,XML 应该(在我看来)是一种非常有限的小众技术,最适合仅用于发明您自己的富文本标记语言,如果您出于某种原因不想使用 HTML。问题是在 1998 年仍然有很多关于 Web 的炒作,而 XML 因其与 HTML 的表面相似而变得流行。尝试将实际上为方便标记而设计的语法应用于分层数据是一种奇怪的设计选择。

回答by Guffa

They are both data formats for hierarchical data, so while the syntax is quite different, the structure is similar. Example:

它们都是分层数据的数据格式,因此虽然语法有很大不同,但结构相似。例子:

JSON:

JSON:

{
  "persons": [
    {
      "name": "Ford Prefect",
      "gender": "male"
    },
    {
      "name": "Arthur Dent",
      "gender": "male"
    },
    {
      "name": "Tricia McMillan",
      "gender": "female"
    }
  ]
}

XML:

XML:

<persons>
  <person>
    <name>Ford Prefect</name>
    <gender>male</gender>
  </person>
  <person>
    <name>Arthur Dent</name>
    <gender>male</gender>
  </person>
  <person>
    <name>Tricia McMillan</name>
    <gender>female</gender>
  </person>
</persons>

The XML format is more advanced than shown by the example, though. You can for example add attributes to each element, and you can use namespaces to partition elements. There are also standards for defining the format of an XML file, the XPATH language to query XML data, and XSLT for transforming XML into presentation data.

不过,XML 格式比示例中显示的更高级。例如,您可以为每个元素添加属性,并且可以使用命名空间来划分元素。还有用于定义 XML 文件格式的标准、用于查询 XML 数据的 XPATH 语言以及用于将 XML 转换为表示数据的 XSLT。

The XML format has been around for some time, so there is a lot of software developed for it. The JSON format is quite new, so there is a lot less support for it.

XML 格式已经存在一段时间了,因此有很多软件针对它开发。JSON 格式相当新,因此对它的支持要少得多。

While XML was developed as an independent data format, JSON was developed specifically for use with Javascript and AJAX, so the format is exactly the same as a Javascript literal object (that is, it's a subset of the Javascript code, as it for example can't contain expressions to determine values).

虽然 XML 是作为一种独立的数据格式开发的,但 JSON 是专门为与 Javascript 和 AJAX 一起使用而开发的,因此该格式与 Javascript 文字对象完全相同(也就是说,它是 Javascript 代码的子集,例如它可以'不包含用于确定值的表达式)。

回答by Mark Baker

The difference between XML and JSON is that XML is a meta-language/markup language and JSON is a lightweight data-interchange. That is, XML syntax is designed specifically to have no inherent semantics. Particular element names don't mean anything until a particular processing application processes them in a particular way. By contrast, JSON syntax has specific semantics built in stuff between {} is an object, stuff between [] is an array, etc.

XML 和 JSON 之间的区别在于 XML 是一种元语言/标记语言,而 JSON 是一种轻量级数据交换。也就是说,XML 语法专门设计为没有固有的语义。在特定处理应用程序以特定方式处理它们之前,特定元素名称没有任何意义。相比之下,JSON 语法在 {} 之间的东西是一个对象,[] 之间的东西是一个数组等中内置了特定的语义。

A JSON parser, therefore, knows exactly what every JSON document means. An XML parser only knows how to separate markup from data. To deal with the meaning of an XML document, you have to write additional code.

因此,JSON 解析器确切地知道每个 JSON 文档的含义。XML 解析器只知道如何将标记与数据分开。要处理 XML 文档的含义,您必须编写额外的代码。

To illustrate the point, let me borrow Guffa's example:

为了说明这一点,让我借用 Guffa 的例子:

{   "persons": [
  {
    "name": "Ford Prefect",
    "gender": "male"
 },
 {
   "name": "Arthur Dent",
   "gender": "male"
  },
  {
    "name": "Tricia McMillan",
    "gender": "female"
  }   ] }

The XML equivalent he gives is not really the same thing since while the JSON example is semantically complete, the XML would require to be interpreted in a particular way to have the same effect. In effect, the JSON is an example uses an established markup language of which the semantics are already known, whereas the XML example creates a brand new markup language without any predefined semantics.

他给出的 XML 等价物实际上并不是一回事,因为虽然 JSON 示例在语义上是完整的,但 XML 需要以特定方式进行解释才能获得相同的效果。实际上,JSON 是一个使用语义已知的已建立标记语言的示例,而 XML 示例创建了一种全新的标记语言,没有任何预定义的语义。

A better XML equivalent would be to define a (fictitious) XJSON language with the same semantics as JSON, but using XML syntax. It might look something like this:

更好的 XML 等价物是定义一种(虚构的)XJSON 语言,其语义与 JSON 相同,但使用 XML 语法。它可能看起来像这样:

<xjson>   
  <object>
    <name>persons</name>
    <value>
      <array>
         <object>
            <value>Ford Prefect</value>
            <gender>male</gender>
         </object>
         <object>
            <value>Arthur Dent</value>
            <gender>male</gender>
         </object>
         <object>
            <value>Tricia McMillan</value>
            <gender>female</gender>
         </object>
      </array>
    </value>   
  </object> 
 </xjson>

Once you wrote an XJSON processor, it could do exactly what JSON processor does, for all the types of data that JSON can represent, and you could translate data losslessly between JSON and XJSON.

一旦您编写了 XJSON 处理器,它就可以完成 JSON 处理器所做的工作,对于 JSON 可以表示的所有类型的数据,并且您可以在 JSON 和 XJSON 之间无损地转换数据。

So, to complain that XML does not have the same semantics as JSON is to miss the point. XML syntax is semantics-free by design. The point is to provide an underlying syntax that can be used to create markup languages with any semantics you want. This makes XML great for making up ad-hoc data and document formats, because you don't have to build parsers for them, you just have to write a processor for them.

因此,抱怨 XML 没有与 JSON 相同的语义是没有抓住重点。XML 语法在设计上是无语义的。重点是提供一种底层语法,可用于创建具有您想要的任何语义的标记语言。这使得 XML 非常适合用于构建临时数据和文档格式,因为您不必为它们构建解析器,您只需为它们编写一个处理器。

But the downside of XML is that the syntax is verbose. For any given markup language you want to create, you can come up with a much more succinct syntax that expresses the particular semantics of your particular language. Thus JSON syntax is much more compact than my hypothetical XJSON above.

但 XML 的缺点是语法冗长。对于您想要创建的任何给定标记语言,您可以提出一个更简洁的语法来表达您的特定语言的特定语义。因此 JSON 语法比我上面假设的 XJSON 紧凑得多。

If follows that for really widely used data formats, the extra time required to create a unique syntax and write a parser for that syntax is offset by the greater succinctness and more intuitive syntax of the custom markup language. It also follows that it often makes more sense to use JSON, with its established semantics, than to make up lots of XML markup languages for which you then need to implement semantics.

如果遵循真正广泛使用的数据格式,创建独特语法和为该语法编写解析器所需的额外时间被自定义标记语言的更简洁和更直观的语法所抵消。此外,使用 JSON 及其已建立的语义通常比组成大量 XML 标记语言更有意义,然后您需要为其实现语义。

It also follows that it makes sense to prototype certain types of languages and protocols in XML, but, once the language or protocol comes into common use, to think about creating a more compact and expressive custom syntax.

因此,在 XML 中对某些类型的语言和协议进行原型设计是有意义的,但是,一旦语言或协议得到普遍使用,就应该考虑创建更紧凑和更具表现力的自定义语法。

It is interesting, as a side note, that SGML recognized this and provided a mechanism for specifying reduced markup for an SGML document. Thus you could actually write an SGML DTD for JSON syntax that would allow a JSON document to be read by an SGML parser. XML removed this capability, which means that, today, if you want a more compact syntax for a specific markup language, you have to leave XML behind, as JSON does.

有趣的是,作为旁注,SGML 认识到了这一点,并提供了一种机制来为 SGML 文档指定减少的标记。因此,您实际上可以为 JSON 语法编写一个 SGML DTD,它允许 SGML 解析器读取 JSON 文档。XML 删除了此功能,这意味着,今天,如果您想要特定标记语言的更紧凑的语法,则必须像 JSON 一样将 XML 抛在脑后。

回答by Michael Mrozek

They're two different ways of representing data, but they're pretty dissimilar. The wikipedia pages for JSONand XMLgive some examples of each, and there's a comparisonparagraph

它们是表示数据的两种不同方式,但它们非常不同。JSONXML的维基百科页面分别给出了一些示例,并且有一个比较段落

回答by Fitzchak Yitzchaki

They are two formats of representation of information. While JSON was designed to be more compact, XML was design to be more readable.

它们是信息表示的两种格式。虽然 JSON 被设计得更紧凑,但 XML 被设计成更具可读性。

回答by Enchantner

XML uses a tag structures for presenting items, like <tag>item</tag>, so an XML document is a set of tags nested into each other. And JSON syntax looks like a construction from Javascript language, with all stuff like lists and dictionaries:

XML 使用标记结构来表示项目,例如 <tag>item</tag>,因此 XML 文档是一组相互嵌套的标记。JSON 语法看起来像是 Javascript 语言的构造,包含列表和字典之类的所有内容:

{
 'attrib' : 'value',
 'array' : [1, 2, 3]
}

So if you use JSON it's really simple to use a JSON strings in many script languages, especially Javascript and Python.

因此,如果您使用 JSON,那么在许多脚本语言中使用 JSON 字符串非常简单,尤其是 Javascript 和 Python。