Detailed explanation of json parsing examples using javascript
Json format generated by php
Usage page
The Code is as follows:
<Script src = "scripts/json. js"> </script>
Alert (data. toJSONString ());
If false is returned, no data is returned.
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 objects.
The Code is as follows:
FunctionmyEval (){
Varstr = '{"name": "Violet", "occupation": "character "}';
Varbj = eval ('+ str + ')');
Alert (obj. toJSONString ());
}
Or use the parseJSON () method.
Copy the Code as follows:
FunctionmyEval (){
Varstr = '{"name": "Violet", "occupation": "character "}';
Varbj = str. parseJSON ();
Alert (obj. toJSONString ());
}