JavaScript parsing JSON instance detailed _javascript tips

Source: Internet
Author: User
Tags eval

The example in this article describes the way JavaScript parses JSON. Share to everyone for your reference. The specific methods are as follows:

Download the JSON library
Http://www.json.org/json-zh.html himself for JavaScript.
Or just go down there.
Https://github.com/douglascrockford/JSON-js

PHP generates JSON format

Working with pages

Copy Code code as follows:
<script src= "Scripts/json.js" ></script>
Alert (data.tojsonstring ());


If return false indicates no data

JS Code:
Copy Code code 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
Copy Code code as follows:
User.username = "Jerry";

You can use Eval to convert JSON characters to object
Copy Code code 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 ());
}

I hope this article will help you with your JavaScript programming.

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.