Javascript parses the json instance in detail


This article illustrates how javascript can parse json. Share with you for your reference. Specific methods are as follows:

Download the json libraries http://www.json.org/json-zh.html your own javascript Or just go down there https://github.com/douglascrockford/JSON-js   PHP generates json format

Use page

<script src="scripts/json.js"></script
alert(data.toJSONString());

  If false is returned, there is no data   Js code:

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

user.username ="jerry";

You can use eval to convert JSON characters to Object

functionmyEval() {  
    varstr = '{"name":"Violet","occupation":"character"}';  
    varbj = eval('(' + str + ')');  
     alert(obj.toJSONString());  
}

  Or use the parseJSON() method

functionmyEval() {  
    varstr = '{"name":"Violet","occupation":"character"}';  
    varbj = str.parseJSON();  
     alert(obj.toJSONString());  
}