JSON.parse()
JSON is usually used to exchange data with the server.
When receiving server data, it is generally a string.
We can use the JSON.parse() method to convert the data into a JavaScript object.
Syntax
JSON.parse(text[, reviver])
Parameter Description:
- **text:** required, a valid JSON string.
- reviver: Optional, a function to transform the result, which will be called for each member of the object.
JSON parsing example
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 into a JavaScript object:
var
obj
=
JSON
.
parse
(
'
{ "name":"runoob", "alexa":10000, "site":"www.runoob.com" }
'
)
;
Before parsing, make sure your data is in standard JSON format, otherwise a parsing error will occur.
You can use our online tool to check:https://www.jyshare.com/front-end/53.
After parsing is complete, we can use the JSON data on the web page:
Example
<p id="demo" p>
<script>var obj = JSON.parse('{ "name":"runoob", "alexa":10000, "site":"www.runoob.com" }'); document.getElementById("demo").innerHTML = obj.name + ":" + obj.site;
script
Try it »
Receiving JSON data from the server
We can use AJAX to request JSON data from the server and parse it into a JavaScript object.
Example
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
"
,
"
/try/ajax/json_demo.txt
"
,
true
)
;
xmlhttp
.
send
(
)
;
Try it »
View server-side data: json_demo.txt
Receiving array JSON data from the server
If the JSON data received from the server is an array, JSON.parse will convert it into a JavaScript array:
Example
var
xmlhttp
=
new
XMLHttpRequest
(
)
;
xmlhttp
.
onreadystatechange
=
function
(
)
{
if
(
this
.
readyState
==
4
&&
this
.
status
==
200
)
{
myArr
=
JSON
.
parse
(
this
.
responseText
)
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
myArr
[
1
]
;
}
}
;
xmlhttp
.
open
(
"
GET
"
,
"
/try/ajax/json_demo_array.txt
"
,
true
)
;
xmlhttp
.
send
(
)
;
Try it »
View server-side data: json_demo_array.txt
exception
Parsing data
JSON cannot store a Date object.
If you need to store a Date object, you need to convert it into a string.
Then convert the string back into a Date object later.
Example
var
text
=
'
{ "name":"Runoob", "initDate":"2013-12-14", "site":"www.runoob.com"}
'
;
var
obj
=
JSON
.
parse
(
text
)
;
obj
.
initDate
=
new
Date
(
obj
.
initDate
)
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
obj
.
name
"
Creating a date:
"
obj
.
initDate
;
Try it »
We can enable the second parameter reviver of JSON.parse, a function to transform the result, called for each member of the object.
Example
var
text
=
'
{ "name":"Runoob", "initDate":"2013-12-14", "site":"www.runoob.com"}
'
;
var
obj
=
JSON
.
parse
(
text
,
function
(
key
,
value
)
{
if
(
key
==
"
initDate
"
)
{
return
new
Date
(
value
)
;
}
else
{
return
value
;
}
}
)
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
obj
.
name
"
Creating a date:
"
obj
.
initDate
;
Try it »
The reviver function
JSON does not allow functions, but you can store a function as a string and then convert the string back into a function later.
Example
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 ranking:
"
obj
.
alexa
(
)
;
Try it »
It is not recommended to use functions in JSON.
Browser support
Mainstream browsers all support the JSON.parse() function:
| Supported | 8.0 | 3.5 | 4.0 | 10.0 |
Older browsers can use a third-party library for support:https://github.com/douglascrockford/JSON-js