Home > Article > Backend Development > JSON, json online parsing_PHP tutorial
Brief description:
JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition - December 1999).
JSON adopts a completely language-independent text format, but also uses conventions similar to the C language family (including C, C, C#, Java, JavaScript, Perl, Python, etc.).
These features make JSON an ideal data exchange language. Easy for humans to read and write, and easy for machines to parse and generate.
JSON syntax:
JSON syntax is a subset of JavaScript syntax.
JSON syntax rules
Data in name/value pairs
Data separated by commas
Curly braces save objects
Arrays saved in square brackets
JSON name/value pairs
The writing format of JSON data is: name/value pairs.
A name/value pair consists of the field name (in double quotes), followed by a colon, and then the value:
1
|
"firstName":"John" |
1 |
firstName="John" |
1 | firstName="John" |
JSON value:
Number (integer or floating point number)
String (in double quotes)
Logical value (true or false)
Array (in square brackets)
Object (in curly braces)
null
Comparison with XML:
1. Readability
The readability of JSON and XML is comparable. One side has simple syntax, and the other side has standardized tag format. It is difficult to distinguish the winner.
2. Scalability
XML is naturally very scalable, and JSON certainly has it. There is nothing that XML can expand that JSON cannot. However, JSON is at home in Javascript and can store Javascript composite objects, which has incomparable advantages over xml.
3. Coding difficulty
XML has a wealth of coding tools, such as Dom4j, JDom, 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.
4. Decoding difficulty
There are two ways to parse XML:
The first is to parse through the document model, that is, to index a set of tags through the parent tag.
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 the 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.
It must be very difficult to parse such scalable structured data.
The same goes for JSON. If you know the JSON structure in advance, using JSON for data transfer is simply wonderful. You can write very practical, beautiful and readable code. 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 not using this string, which 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, thereby reducing the same data traffic. Lower the transmission pressure of the network.