Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

3 ways JSON data is parsed


May 08, 2021 JSON


Table of contents


Overview

The JSON format is now getting more and more attention in web development, especially when using ajax development projects, and it is often necessary to return the string of the Json format to the front end, which resolves to the JS object (JSON).
The concept of JSON is not included in the standard in ECMA-262(E3), but the concept of JSON is formally introduced in ECMA-262(E5), including the global JSON object and Date's toJSON method.
The three methods for parsing JSON data are described below:

eval() method

The most common way to parse JSON data is to use javascript's eval() method, as follows:

The code is as follows:
function toJson(str){
 var json = eval('(' + str + ')');
 return json;
}

This method has performance and security issues and is not recommended.

The new Function method

The code is as follows:
function toJson(str){
 var json = (new Function("return " + str))();
 return json;
}

JSON.parse() method

This method only supports versions such as IE8/Firefox3.5 plus/Chrome4/Safari4/Opera10, which are already close to the W3C standard and implement the toJSON method by default.
The code is as follows:
function toJson(str){
 return JSON.parse(str);
}

json2 .js will use the native version when the browser natively supports JSON.parse, and it is API compatible with ES5. W ith ES5 not yet fully available, John Resig recommends jason 2.js mainly so that you can now use an ES5-compatible API and make a smooth transition to ES5 in the future - just remove an import and swap it.

Javascript parses the json instance in detail

Download the json library
http://www.json.org/json-zh.html to find javascript yourself
Or go straight below
https://github.com/douglascrockford/JSON-js

Php generates the johnson format

Use the page

The code is as follows:

<script src="scripts/json.js"></script>  
alert(data.toJSONString());
If you return false, there is no data

js code

The code is as follows:

functionshowJSON() {   
   varuser =   
     {   
       "username":"tom",   
       "age":20,   
        "info": {"tel":"123456","cellphone":"98765"},   
      "address":   
       [   
                {"city":"shanghai","postcode":"201203"},   
                 {"city":"suzhou","postcode":"200000"}   
             ]   
     }   
       
     alert(user.username);   
     alert(user.age);   
     alert(user.info.cellphone);   
     alert(user.address[0].city);   
     alert(user.address[0].postcode);   
}

Modify

The code is as follows:

user.username ="jerry";

You can use eval to convert JSON characters to Object

The code is as follows:

functionmyEval() {   
    varstr = '{"name":"Violet","occupation":"character"}';   
    varbj = eval('(' + str + ')');   
     alert(obj.toJSONString());   
}
Or use the parseJSON() method
The code is as follows:
functionmyEval() {   
    varstr = '{"name":"Violet","occupation":"character"}';   
    varbj = str.parseJSON();   
     alert(obj.toJSONString());   
}