json和xml之间的区别是什么 [英] what is the difference between json and xml

查看:128
本文介绍了json和xml之间的区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

json和xml之间的区别

what is the difference between json and xml

推荐答案

没有其他答案似乎提到的根本区别是XML是一种标记语言(它实际上在其名称中说),而JSON是一种表示对象(也在其名称中指出)的方式。

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

语言是向自由流动的纯文本添加额外信息的方式,例如

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

Here is some text.

使用XML(使用某个元素词汇表),您可以输入:

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

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

这使得标记语言对于表示文档非常有用。

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

像JSON这样的对象符号不够灵活。但这通常是一件好事。当你表示对象时,你不需要额外的灵活性。要在JSON中表示上面的例子,你实际上必须手动解决一些XML为你解决的问题。

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.

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

不像XML那么好,原因是我们试图用对象符号做标记。因此,我们必须发明一种方法,使用可以容纳字符串和嵌套对象的混合的内容数组,在我们的对象周围分散纯文本的片段。

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.

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

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.

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

逻辑上等效的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看起来更像我们在编程语言中声明的数据结构。它也有较少的重复的名称。

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

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

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具有元素对属性。这看起来像同样的区别,但它不是,因为属性只能有字符串值。它们不能是嵌套对象。所以我不能应用这个想法到< 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中区分列表和记录。 JSON的优点是低级语法具有内置的区别,所以它非常简洁和通用。这意味着JSON默认情况下更自描述,这是两种格式的重要目标。

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.

所以JSON应该是对象符号的第一选择,其中XML

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

不幸的是,对于XML,我们已经有HTML作为世界上第一个富文本标记语言。试图用XML来重新定义HTML,但是没有太多的优势。

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的某些原因。问题是在1998年仍然有很多关于Web的炒作,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.

这篇关于json和xml之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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