Home>

What is json?

json (javascript object notation) is a lightweight data exchange format,In a language-independent text format,Is the ideal data exchange format,Meanwhile, json is a javascript native format.

Ideal for server-javascript interaction

Why use json instead of xml

They all say this:Although there is a lot of hype about how xml is cross-platform,Cross-language advantage,However, unless applied to web services, in ordinary web applications, developers often have troubles in parsing XML.Whether it is server-side generation or processing of xml, or client-side parsing of xml with javascript, it often leads to complex code,Extremely low development efficiency.In fact, for most web applications,They don't need complex xml to transfer data at all,The scalability of xml has few advantages,Many ajax applications even return html fragments directly to build dynamic web pages. Compared to returning xml and parsing it,Returning html snippets greatly reduces the complexity of the system,But it lacks some flexibility

How to use

The following code is an html code snippet,Implement click button to parse json format data and alert content

<input type="button" value="button" onclick="clicks ();" />

Here is the js function code:

var json={
  contry:{
  area:{
   man:"120,000",   women:"100,000"
  }
  }
 };
//Method 1:Use eval parsing
 var obj=eval (json);
 alert (obj.constructor);
 alert (obj.contry.area.women);
 //Method 2:Use the funtion function
 var strjson="{name:" json name "}";//The resulting json
 var obj=new function ("return" + strjson) ();//The converted json object
 alert (obj.name);//json name
 alert (obj.constructor);
//A bit more complicated analysis of 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":"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 more complicated 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);
}

Fourth, eval

① This form will significantly reduce 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 evaluated text.Just like the with statement is executed,It reduces the performance of the language

③ function constructor is another form of eval,So it should also be avoided.

  • Previous Example of PHP's sprintf function based on JS
  • Next The difference and connection between JavaScript and HTML DOM and the relationship between Javascript and DOM