1. 1. JSON
  2. 2. XML
  3. 3. Differences
  4. 3.1 XML is a markup language whereas JSON is a way of representing objects
    1. 3.2 JSON has defined ways of distinguishing records


JSON - JavaScript Object Notation, an open standard file format uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value). It is a very common data format used for asynchronous browser–server communication, including as a replacement for XML in some AJAX-style systems.

The official Internet media type for JSON is application/json.

2. XML

JSON is promoted as a low-overhead alternative to XML as both of these formats have widespread support for creation, reading, and decoding in the real-world situations where they are commonly used.

XML has been used to describe structured data and to serialize objects. Various XML-based protocols exist to represent the same kind of data structures as JSON for the same kind of data interchange purposes. Data can be encoded in XML in several ways. The most expansive form using tag pairs results in a much larger representation than JSON, but if data is stored in attributes and ‘short tag’ form where the closing tag is replaced with ‘/>’, the representation is often about the same size as JSON or just a little larger. If the data is compressed using an algorithm like gzip, there is little difference because compression is good at saving space when a pattern is repeated.

XML also has the concept of ++schema++. This permits strong typing, user-defined types, predefined tags, and formal structure, allowing for formal validation of an XML stream in a portable way. Similarly, there is an IETF draft proposal for a schema system for JSON.[44]

XML supports comments, but JSON does not

3. Differences

3.1 XML is a markup language whereas JSON is a way of representing objects

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

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

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

JSON is better suited if you have typical a hierarchy of objects and you want to represent them in a stream.

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

Below is same expression in xml

        <Relative>The Boy</Relative>
        <Relative>I think that's all of them</Relative>

3.2 JSON has defined ways of distinguishing records

You can differenciate records directly in JSON. List and normal record have different expression; wheareas in XML, they look all same.

We need to use an external schema or extra user defined attributes in XML to express different expressions, or some limitations on it. While in JSON, it’s self describing by default.

-> JSON should be the first choise for object notation, where XML should spot at document markup.

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 stone2paul@gmail.com



本文作者:Leilei Chen

发布时间:2020-02-09, 14:25:58

最后更新:2020-02-09, 14:26:25


版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。