JSON ------ uncover the mystery of the similarities and differences between XML and JSON []

JSON: JavaScript Object Notation (JavaScript Object Notation).  JSON syntax for storing and exchanging text messages. Similar to XML, but it is smaller than XML, faster, and easier to resolve.
 
XML
The XML ( Extensible Markup Language)  refers to Extensible Markup Language, which is designed to transmit and store data.
To indicate a object (refer to a collection of name-value pair), the first element may be used as the Object, each of the key-value pair attribute represented by
 
<student>
    <name>John</name>
    <age>10</age>
    <address>
        <country>China</country>
        <province>Guang Dong</province>
        <city>shanghai</city>
        <district>songbin</district>
    </address>
</student>

 

JSON

JavaScript ( JavaScript Object Notation)  object representation. JSON syntax for storing and exchanging text messages. Similar to XML, but it is smaller than XML, faster, and easier to resolve.

{
    "student": {
      "name": "John",
      "age": "10",
      "address": {
        "country": "China",
        "province": "Guang Dong",
        "city": "shanghai",
        "district": "songbin"
      }
    }
}
 
In fact, AJAX birth, it is called XMLHTTP, until after 2005, was officially named as AJAX. However, as time goes on, AJAX drawbacks gradually revealed:
 
Because the implementation is based on XML DOM tree implementation, and DOM implementation details in a variety of browsers are not the same, so the XML cross-browser compatibility is not good,
So the need for a new data payload format integrated into the HTML pages to meet the requirements of Ajax!

How can integrate data into HTML and resolve browser compatibility problems? The answer is: the use of all major browsers in a general assembly --JavaScript engine.
So long as to create a JavaScript engine can identify the data format can be friends!
 
 
Contrast XML and JSON:
1. XML is an element, attribute and element content. 
   JSON is object, array, string, number, boolean (true / false) , and null. 

2. XML element content need to choose how to deal with the new line, 
   JSON String is not subject to this choice. 

3. XML text only, no pre-digital format, 
   JSON is a clear number format, so that the locale is safe. 

4. XML is written on the first line you need to define the version of XML, 
   JSON version of the problem does not exist, the format will never change! 

5. XML mapping array no major problems is that redundant array element tag comparison is repeated. 
   JSON relatively easy to read. JSON the true / false / null can be easily unified to a corresponding general programming language semantics.

 

XML and JSON conversion address: http://www.bejson.com/xml2json/

 
 
 


 
【note】:
1. the JSON overall need in quotes , a character string ,
2. JSON keys need to be enclosed in quotes :
因为JS中存在许多的关键字和保留关键字,只要我们在所有的键名上加上双引号,JS引擎会将其识别为字符串,这样也就避免与JS中关键字冲突!
 
var jsonobj = `{
  "student": {
    "name": "John",
    "age": "10",
    "address": {
      "country": "China",
      "province": "Guang Dong",
      "city": "shanghai",
      "district": "songbin"
    }
  }
}`

var result = JSON.parse(jsonobj)
document.write(result) // 此时网页中会输出: [object Object]

【注意】:

当我们要把数据展示到UI界面上时,必须先将 JS 对象转化为 JSON 对象(字符串),这个过程又被称为“序列化”

序列化是指把对象转换为字节序列的过程,而反序列化是指把字节序列恢复为对象的过程

 

Guess you like

Origin www.cnblogs.com/edwardwzw/p/12054392.html