A common use of JSON is to exchange data to/from a web server.
When receiving data from a web server, the data is always a string.
Imagine we received this text from a web server:
Make sure the text is written in JSON format, or else you will get a syntax error.
You can request JSON from the server by using an AJAX request
Use the XMLHttpRequest to get data from the server:
Take a look at json_demo.txt
The JSON returned from the server is an array:
Take a look at json_demo_array.txt
Date objects are not allowed in JSON.
If you need to include a date, write it as a string.
You can convert it back into a date object later:
Convert a string into a date:
Or, you can use the second parameter, of the JSON.parse() function, called reviver.
The reviver parameter is a function that checks each property, before returning the value.
Convert a string into a date, using the reviver function:
Functions are not allowed in JSON.
If you need to include a function, write it as a string.
You can convert it back into a function later:
Convert a string into a function:
You should avoid using functions in JSON, the functions will lose their scope, and you would have to use eval() to convert them back into functions.
|Web Browsers Support|