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:
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.