js reads and parses JSON type data

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. At the same time, JSON is a native JavaScript format. Well
suited for server interaction with JavaScript
2. Why JSON and not XML
In common web applications, developers often struggle with XML parsing. Whether it is generating or processing XML on the server side or parsing XML on the client side with JavaScript, it often leads to complex codes and extremely low development efficiency. In fact, for most Web applications, they don't need complex XML to transfer data at all, XML's extensibility is rarely an advantage, and many AJAX applications even return HTML fragments directly 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 a certain degree of flexibility
3. How to use

<input type="button" value="button" onclick="clicks();"/>
The following is the js function code:
var json = {
   contry:{
    area:{
     man:"12万",
     women:"10万"
    }
   }
  };
//Method 1: use eval parsing
  var obj = eval(json);
  alert(obj.constructor);
  alert(obj.contry.area.women);
  //Method 2: Use the Function function
  var strJSON = "{name:'json name'}";//Get JSON
  var obj = new Function("return" + strJSON)();//The converted JSON object
  alert(obj.name);//json name
  alert(obj.constructor);
  
//Analysis of 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":"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 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);
 }


4. This form of eval
1 will significantly degrade performance because it has to run the compiler. 2 The
eval function also reduces the security of your application because it gives too much power to the text being evaluated. Like the way the with statement executes, it degrades the performance of the language. The
Function constructor is another form of eval, so it should also be avoided.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326477629&siteId=291194637