JavaScript parsing JSON instance detailed

Source: Internet
Author: User
Tags eval json

PHP generates JSON format

Working with pages

code is as follows: <script src= "Scripts/json.js" ></script>


alert (data.tojsonstring ());



If return false indicates 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

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

copy code code as follows: Functionmyeval () {


varstr = ' {' name ': ' Violet ', ' occupation ': ' character '} ';


VARBJ = Str.parsejson ();


alert (obj.tojsonstring ());


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.