JSON is typically used to exchange data with the server.
is typically a string when receiving server data.
We can use the Json.parse () method to convert data to JavaScript objects.
Grammar
Json.parse (text[, Reviver])
Parameter description:
- text: required, a valid JSON string.
- reviver: Optional, a function that transforms the result, which will call this function for each member of the object.
JSON Parsing Instance
For example, we received the following data from the server:
{"Name": "Runoob", "Alexa": 10000, "site": "Www.runoob.com"}
We use the Json.parse () method to process the above data and convert it to a JavaScript object:
var obj = json.parse (' {' name ': ' Runoob ', ' Alexa ': 10000, ' site ': ' www.runoob.com '} ');
Once the parsing is complete, we can use the JSON data on the Web page:
For example:
<PID= "Demo"></P> <Script>varobj=Json.parse ('{"name": "Runoob", "Alexa": 10000, "site": "Www.runoob.com"}');d Ocument.getelementbyid ("Demo"). InnerHTML=Obj.name+ ":" +Obj.site;</Script>
Receiving JSON data from the server
We can use AJAX to request JSON data from the server and parse it into JavaScript objects.
var xmlhttp = new XMLHttpRequest (); xmlhttp.onreadystatechange = function () { if (this.readystate = = 4 && this. Status = = $) { MYOBJ = Json.parse (this.responsetext); document.getElementById ("Demo"). InnerHTML = Myobj.name; }}; Xmlhttp.open ("GET", "/try/ajax/json_demo.txt", true); Xmlhttp.send ();
Receiving JSON data from the service side of an array
If the JSON data for an array is received from the server, Json.parse converts it to a JavaScript array:
Exception parsing data
JSON cannot store Date objects.
If you need to store a Date object, you need to convert it to a string.
The string is then converted to a Date object.
L For example:
var text = ' {' name ': ' Runoob ', ' initdate ': ' 2013-12-14 ', ' site ': ' www.runoob.com '} '; var obj = json.parse (text); O Bj.initdate = new Date (obj.initdate); document.getElementById ("Demo"). InnerHTML = Obj.name + "Date Created:" + obj.initdate;
We can enable json.parse for the second parameter reviver, a function that transforms the result, which each member of the object calls this function.
var text = ' {' name ': ' Runoob ', ' initdate ': ' 2013-12-14 ', ' site ': ' www.runoob.com '} '; var obj = json.parse (text, function (ke Y, value) { if (key = = "Initdate") { return new Date (value); } else { return value;}}); document.getelemen Tbyid ("Demo"). InnerHTML = Obj.name + "Date Created:" + obj.initdate;
Analytic functions
JSON does not allow a function to be included, but you can store the function as a string and then convert the string to a function.
var text = ' {' "name": "Runoob", "Alexa": "function () {return 10000;}", "Site": "www.runoob.com"} '; var obj = json.parse (text) ; obj.alexa = eval ("(" + Obj.alexa + ")"); document.getElementById ("Demo"). InnerHTML = Obj.name + "Alexa rank:" + obj.alexa ();
Browser support
The Json.parse () function is supported by mainstream browsers:
- Firefox 3.5
- Internet Explorer 8
- Chrome
- Opera 10
- Safari 4
Json.parse ()--json turn JS