JSON.parse()
- Previous Page JSON Data Types
- Next Page JSON Serialization
The conventional use of JSON is for data transmission with web servers.
When receiving data from a web server, the data is always a string.
Through JSON.parse()
Parse the data, which will become JavaScript objects.
Example – Parsing JSON
Imagine that we receive this text from a web server:
'{ "name":"Bill Gates", "age":62, "city":"Seattle"}'
Please use the JavaScript function JSON.parse()
Convert text to JavaScript object:
var obj = JSON.parse('{ "name":"Bill Gates", "age":62, "city":"Seattle"}');
Make sure this text is written in JSON format, otherwise there will be syntax errors.
Please use JavaScript objects on your page:
Example
<p id="demo"></p> <script> document.getElementById("demo").innerHTML = obj.name + ", " + obj.age; </script>
JSON from the server
You can request JSON from the server by using AJAX requests.
As long as the server's response is written in JSON format, you can parse the string into a JavaScript object.
Example
Please use XMLHttpRequest to get data from the server:
var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myObj = JSON.parse(this.responseText); document.getElementById("demo").innerHTML = myObj.name; } }; xmlhttp.open("GET", "json_demo.txt", true); xmlhttp.send();
as an array of JSON
when using JSON derived from an array JSON.parse()
After that, this method will return a JavaScript array instead of a JavaScript object.
Example
The JSON returned from the server is an array:
var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myArr = JSON.parse(this.responseText); document.getElementById("demo").innerHTML = myArr[0]; } }; xmlhttp.open("GET", "json_demo_array.txt", true); xmlhttp.send();
Exception
Parse date
Date objects are not allowed in JSON.
If you need to include a date, write it as a string.
After that, you can convert it back to a date object:
Example
Convert a string to a date:
var text = '{ "name":"Bill Gates", "birth":"1955-10-28", "city":"Seattle"} var obj = JSON.parse(text); obj.birth = new Date(obj.birth); document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth;
or you can use JSON.parse()
The second parameter of the function, known as reviver.
this reviver The parameter is a function, which checks each property before returning the value.
Example
Convert a string to a date, using the reviver function:
var text = '{ "name":"Bill Gates", "birth":"1955-10-28", "city":"Seattle"}'; var obj = JSON.parse(text, function (key, value) { if (key == "birth") { return new Date(value); } else { return value; }); document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth;
Parse function
Functions are not allowed in JSON.
If you need to include a function, write it as a string.
Later you can convert it back to a function:
Example
Convert a string to a function:
var text = '{ "name":"Bill Gates", "age":"function () {return 62;}", "city":"Seattle"} var obj = JSON.parse(text); obj.age = eval("(" + obj.age + ")"); document.getElementById("demo").innerHTML = obj.name + ", " + obj.age();
You should avoid using functions in JSON, as functions will lose their scope, and you will also need to use eval()
Convert them back to functions.
Browser Support
All mainstream browsers and the latest ECMAScript (JavaScript) standards include JSON.parse()
Function:
The numbers in the following table specify full support JSON.parse()
The first browser version of the function:
Yes | 8.0 | 3.5 | 4.0 | 10.0 |
For older browsers, the following addresses contain JavaScript libraries that are available:
- Previous Page JSON Data Types
- Next Page JSON Serialization