JSON Chinese Manual

Read(25590) update time(2022-04-13)

In the "JSON Chinese Manual", JSON: JavaScript Object Notation (JavaScript Object Notation) JSON is a syntax for storing and exchanging text information. Similar to XML. JSON is smaller, faster, and easier to parse than XML.


JSON (JavaScript Object Notation, JS Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript (the js specification developed by the European Computer Association) and uses a text format that is completely independent of programming languages ​​to store and represent data. Simplicity and clear hierarchical structure make JSON an ideal data exchange language. It is easy for people to read and write, and it is also easy for machines to parse and generate, and effectively improves network transmission efficiency.

Tip: Before you start learning JSON, you should be familiar with javascript , AJAX , jQuery Knowledge has a basic understanding.

JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy for humans to read and write. It is also easy for machines to parse and generate. It is based on JavaScript Programming Language, a subset of Standard ECMA-262 3rd Edition - December 1999.

JSON is a data format that Douglas Crockford began to promote in 2001. It officially became a mainstream data format from 2005 to 2006. It was at that time that Yahoo and Google began to use the JSON format extensively.

Let us experience the first json instance!

First JSON instance

Instance

<!DOCTYPE html>
<html>
<body>
<h2>JSON Object Creation in JavaScript</h2>
<p>
Name: <span id="jname"></span><br />
Age: <span id="jage"></span><br />
Address: <span id="jstreet"></span><br />
Phone: <span id="jphone"></span><br />
</p>

<script>
var JSONObject= {
"name":"John Johnson",
"street":"Oslo West 555",
"age":33,
"phone":"555 1234567"};
document.getElementById("jname").innerHTML=JSONObject.name
document.getElementById("jage").innerHTML=JSONObject.age
document.getElementById("jstreet").innerHTML=JSONObject.street
document.getElementById("jphone").innerHTML=JSONObject.phone
</script>

</body>
</html>

Run instance»

Click "Run instance" " button to view online examples

Tips: Our JSON tutorial will help you learn JSON knowledge from beginner to advanced. If you have any questions, please go to the PHP Chinese website JSON Community to ask your question, and enthusiastic netizens will answer it for you.

Comparison of JSON and XML

  • Readability

The readability of JSON and XML is very different In comparison, one side has simple syntax and the other side has standardized tag format, so it is difficult to distinguish the winner.

  • Extensibility

XML is naturally very scalable, and JSON certainly has it. There is nothing that XML can extend that JSON cannot. expanded. However, JSON is at home in Javascript and can store Javascript composite objects, which has incomparable advantages over xml.

  • Coding difficulty

XML has a wealth of coding tools, such as Dom4j, Dom, SAX, etc., and JSON also provides tools. Without tools, I believe that skilled developers can quickly write the desired XML documents and JSON strings. However, XML documents require many more structural characters.

  • Decoding Difficulty

There are two ways to parse XML:

One is to parse through the document model, that is, through The parent tag indexes out a set of tags. For example: xmlData.getElementsByTagName("tagName"), but this must be used when the document structure is known in advance and cannot be universally encapsulated.

Another method is to traverse nodes (document and childNodes). This can be achieved through recursion, but the parsed data is still in different forms and often cannot meet the pre-existing requirements.

Any such extensible structured data must be very difficult to parse.

The same is true for JSON. If you know the JSON structure in advance, using JSON for data transfer is simply wonderful. You can write code that is very practical, beautiful and readable. If you are a pure front-end developer, you will definitely like JSON very much. But if you are an application developer, you don't like it so much. After all, xml is the real structured markup language, used for data transfer.

And if you don’t know the structure of JSON and parse JSON, it would be a nightmare. Not only is it time-consuming and labor-intensive, the code will also become redundant and protracted, and the results obtained will be unsatisfactory. But this does not affect many front-end developers choosing JSON. Because toJSONString() in json.js can see the string structure of JSON. Of course, for people who don't often use this string, doing so is still a nightmare. After people who often use JSON see this string, they will have a clear understanding of the structure of JSON, and it will be easier to operate JSON.

The above is the parsing of xml and JSON only for data transmission in Javascript. In the field of Javascript, JSON is the home field after all, and its advantages are of course far superior to xml. If Javascript composite objects are stored in JSON and their structure is not known, I believe many programmers will also cry when parsing JSON.

In addition to the above, another big difference between JSON and XML is the effective data rate. JSON is more efficient when transmitted as a data packet format. This is because JSON does not require strict closing tags like XML, which greatly increases the ratio of effective data volume to total data packets, thus reducing the situation of the same data traffic. Lower the transmission pressure of the network.

Advantages and disadvantages of JSON:

Advantages:

  • The data format is relatively simple, easy to read and write, the formats are compressed, and it takes up little bandwidth;

  • Easy to parse, client-side JavaScript can simply read JSON data through eval();

  • Supports multiple languages, including ActionScript, C, C#, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby and other server-side languages ​​facilitate server-side parsing;

  • In the PHP world, there is already PHP-JSON And JSON-PHP appeared, preferring to call PHP serialized programs directly. PHP server-side objects, arrays, etc. can directly generate JSON format to facilitate client access and extraction;

  • Because The JSON format can be directly used by server-side code, which greatly simplifies the code development of server-side and client-side, accomplishes the same task, and is easy to maintain.

Disadvantages:

  • It is not as popular and widely used as XML format, and it is not as versatile as XML;

  • The promotion of JSON format in Web Service is still in its infancy.

Content covered by this JSON tutorial manual

This JSON tutorial covers all basic and advanced knowledge of JSON, including JSON format, JSON parsing, JSON traversal, JSON calling, JSON conversion, JSON acquisition, JSON string, JSON array, etc.

Tips: Each chapter of this tutorial contains many JSON examples. You can directly click the "Run Example" button to view the results online. These examples will help you better understand and use JSON.

Latest chapter


PHP JSON 2016-10-19
JSONP 教程 2016-10-19
JSON 使用 2016-10-19
JSON 语法 2016-10-19
JSON - 简介 2016-10-19
JSON 教程 2016-10-19