js method for reading and parsing JSON type data

  • 2020-10-23 20:52:18
  • OfStack

This article illustrates how js reads and parses JSON type data. To share for your reference, the details are as follows:

1. What is JSON?

JSON(JavaScript Object Notation) is a lightweight data interchange format that USES a completely language-independent text format and is ideal for data interchange, while JSON is the native JavaScript format.
Ideal for server interaction with JavaScript

2. Why JSON instead of XML

They are all this to say: although there are a lot of publicity about how XML has cross-platform and cross-language advantage, however, unless used in Web Services, otherwise, in ordinary Web applications, developers often a heartbreaker for XML parsing, whether it is generated on the server or processing XML, or client with analytical XML JavaScript, often lead to complex code, the development of very low efficiency. In fact, for most Web applications, there is no need for complex XML to transfer data at all, XML's extensibility offers few advantages, and many AJAX applications even return HTML fragments directly to build dynamic Web pages. Returning HTML fragments significantly reduces the complexity of the system compared to returning XML and parsing it, but at the same time it lacks 1 point of flexibility

3. How to use it

The following code is the html code snippet, which realizes clicking the 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:"12 wan ",
   women:"10 wan "
  }
  }
 };
// way 1 Use: eval parsing 
 var obj = eval(json);
 alert(obj.constructor);
 alert(obj.contry.area.women);
 // way 2 Use: Funtion function 
 var strJSON = "{name:'json name'}";// To get the JSON
 var obj = new Function("return" + strJSON)();// The transformed JSON object 
 alert(obj.name);//json name
 alert(obj.constructor);
// complex 1 Some of the 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);
 // complex 1 Some of the json On the other 1 Kind of form 
 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. eval

This form will make the performance significantly lower because it has to run the compiler

The eval function also weakens the security of your application because it gives too much power to the text being evaluated. Like the with statement executed in the same way, it degrades the language's performance

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

I hope this article has been helpful in JavaScript programming.


Related articles: