Js reads and parses JSON data. js reads and parses json data.

Source: Internet
Author: User

Js reads and parses JSON data. js reads and parses json data.

This example describes how to read and parse JSON data in JavaScript. We will share this with you for your reference. The details are as follows:

1. What is JSON?

JSON (JavaScript Object Notation) is a lightweight data exchange format. It adopts a completely language-independent text format and is an ideal data exchange format. JSON is also a JavaScript native format.
Very suitable for server-to-JavaScript Interaction

Ii. Why JSON instead of XML

They all say this: Although there are many ideas about how XML has the cross-platform and cross-language advantages, unless it is applied to Web Services, in common Web applications, developers often use their brains for XML parsing. Whether the server generates or processes XML, or the client uses JavaScript to parse XML, it often leads to complicated code and extremely low development efficiency. In fact, for most Web applications, they do not need complicated XML to transmit data at all, and XML scalability is rarely advantageous. Many AJAX applications even directly return HTML fragments to build dynamic Web pages. Compared with returning XML and parsing it, returning HTML fragments greatly reduces the complexity of the system, but at the same time lacks some flexibility.

Iii. How to Use

The following code is an html code snippet. You can click the button to parse the json format data and alert content.
Copy codeThe Code is as follows: <input type = "button" value = "button" onclick = "clicks ();"/>
Below is the js function code:

Var json = {contry: {area: {man: "0.12 million", women: "0.1 million" }}; // Method 1: use eval to parse var obj = eval (json); alert (obj. constructor); alert (obj. contry. area. women); // Method 2: Use the Funtion function var strJSON = "{name: 'json name '}"; // JSON var obj = new Function ("return" + strJSON) (); // converted JSON object alert (obj. name); // json name alert (obj. constructor); // json array data parsing var value1 = [{"c01": "1", "c02": "2", "c03 ": "3", "c04": "4", "c05": "5", "c06": "6", "c07": "7", "c08 ": "8", "c09": "9" },{ "c01": "2", "c02": "4", "c03": "5 ", "c04": "2", "c05": "8", "c06": "11", "c07": "21", "c08": "1 ", "c09": "12" },{ "c01": "5", "c02": "1", "c03": "4", "c04 ": "11", "c05": "9", "c06": "8", "c07": "1", "c08": "8", "c09 ": "2"}]; var obj1 = eval (value1); alert (obj1 [0]. c01); // another form of complex json var value2 = {"list": [{"password": "1230", "username ": "coolcooldool" },{ "password": "thisis2", "username": "okokok"}], "array": [{"password": "1230 ", "username": "coolcooldool"}, {"password": "thisis2", "username": "okokok"}]}; var obj2 = eval (value2 ); alert (obj2.list [0]. password );}

Iv. eval

① This form will significantly reduce the performance because it must run the Compiler

② The eval function also compromises the security of your application because it gives too much power to the evaluated text. Just like with statement execution, it reduces the language performance.

③ Function constructor is another form of eval, so it should also be avoided.

I hope this article will help you design JavaScript programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.