Json-based online Parsing
1. What is it?
JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy for reading and writing, and easy for machine parsing and generation. It is a subset of JavaScript, A JavaScript Object Notation, and a syntax for storing and exchanging text information. JSON, similar to XML, is smaller, faster, and easier to parse than XML.
Ii. Syntax
Json syntax is a subset of JavaScript Object Notation:
1. Data in name/value pairs
2. Data is separated by commas (,).
3. Brackets save objects
4. Square brackets Save the Array
If you understand these rules, then the problem arises again? What are the types of Json values? Next we will share with you:
Json value types can be numbers (integer or floating-point number), strings (in double quotation marks), logical values (true or false), arrays (in square brackets), object (in curly brackets), null.
Iii. convert to a Javascript Object
One of the most common usage of Json is to read Json data (as a file or as HttpRequest) from the web server, convert Json data into JavaScript objects, and then use the data on the web page. So how can we convert the Json syntax into the target object? The following describes two methods:
1. eval () function
Eval () is a Javascript function that converts Json text into Javascript objects.
The eval () function uses a JavaScript compiler that parses Json text and generates JavaScript objects. Text must be enclosed in parentheses to avoid syntax errors such:
<span style="font-size:24px;"> var txt = '{"employees" : [' +'{"firstName":"Bill" , "lastName":"Gates"},' +'{"firstName":"George" ,"lastName":"Bush" },' +'{"firstName":"Thomas" ,"lastName":"Carter" } ]}';</span>
Conversion:
var obj = eval ("(" + txt +")");
2. Json parser
The eval () function can compile and execute any JavaScript code. This hides a potential security issue.
It is safer to use the Json parser to convert Json into JavaScript objects. The Json parser can only recognize Json text, rather than compile scripts. In the browser, this provides native Json support, and the Json parser is faster. I will not go into details here because I have never used it!
Iv. Comparison with XML
1. readability.
Json and XML have the same data readability. Json and XML have the same readability. One side is the suggested syntax, and the other side is the standard tag format. XML is more readable.
2. scalability.
XML is inherently highly scalable, and Json is also available. There is nothing XML can be extended, and Json cannot.
3. Encoding difficulty.
XML has rich coding tools, such as Dom4j and JDom, and Json also provides tools provided by json.org. However, Json encoding is much easier than XML, json code can be written without tools, but it is not easy to write XML.
4. Difficulty in decoding.
XML parsing takes into account the parent node of the child node, which is confusing, and the difficulty of Json Parsing is almost 0. There is nothing to say about XML.
V. Summary
I should have known these things in detail for a long time, but I think it is because I am lazy and know that this is the case. When I know a few things, I think it's all back. I don't feel regret until now that my work is used. Therefore, if you encounter new knowledge points in your future study, you must understand them so that you can use them easily.