This article illustrates the way JS reads and parses JSON-type data. Share to everyone for your reference, specific as follows:
One, what is JSON?
JSON (JavaScript Object notation) is a lightweight data interchange format that takes a completely language-independent text format and is an ideal data interchange format, while JSON is the native form of JavaScript.
Ideal for server interaction with JavaScript
Second, why use JSON instead of XML
That's what they say: although there is a lot of publicity about how XML has cross-platform, Cross-language advantages, however, unless applied to web Services, developers often have a brain for XML parsing in ordinary Web applications, whether it's server-side generation or processing XML, or client JavaScript parsing XML, often leads to complex code, very low development efficiency. In fact, for most Web applications, they do not need complex XML to transmit data, XML extensibility has little advantage, and many AJAX applications even directly return HTML fragments to build dynamic Web pages. The return HTML fragment greatly reduces the complexity of the system compared to returning XML and parsing it, but at the same time lacks some flexibility
Third, how to use
The following code is an HTML code fragment that implements the Click button to parse JSON-formatted data and alert content
Copy Code code as follows:
<input type= "button" value= "button" onclick= "clicks ();" />
Here is the JS function code:
var json = {contry:{area:{man: "120,000", Women: "100,000"}}};
Way one: Use eval to parse var obj = eval (JSON);
alert (obj.constructor);
alert (obj.contry.area.women);
Mode two: Using the funtion function var Strjson = "{name: ' JSON name '}";//Get json var obj = new Function ("return" + Strjson) ();//Converted JSON object
alert (obj.name);//json name alert (obj.constructor); Parsing of a complex JSON array data 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": "One", "c07": "The", "C08": "1", "c09": "12"}, {"
C01 ":" 5 "," C02 ":" 1 "," C03 ":" 4 "," C04 ":" One "," C05 ":" 9 "," C06 ":" 8 "," C07 ":" 1 "," C08 ":" 8 "," c09 ":" 2 "}];
var obj1 = eval (value1);
alert (OBJ1[0].C01); Another form of the complex json var value2 = {"List": [{"Password": "1230", "username": "Coolcooldool"}, {"Password": "Thisis2", " Username ":" Okokok "}]," array ": [{" Password ":" 1230 "," username ":" Coolcooldool "}, {" Password ":" Thisis2 "," Usernam
E ":" Okokok "}]}; VarObj2 = eval (value2);
alert (Obj2.list[0].password);
}
Iv. Eval
① This form will significantly degrade performance because it must run the compiler
The ②eval function also weakens the security of your application because it gives too much power to the text being evaluated. As with statement execution, it lowers the performance of the language
The ③function constructor is another form of eval, so it should also be avoided.
I hope this article will help you with JavaScript programming.