JavaScript Kit > JavaScript Reference > Here
JSON Object
JSON, or JavaScript Object Notation, has become a popular alternative to XML for defining structured data using JavaScript. A sample JSON definition that stores some information about Bob may look like the following:
{
"name": "Bob Miller",
"age": 27,
"occupation": "Programmer",
"contact":{
"address": "253 Johnson Road",
"Home_Phone": "544-6443",
"Cell_Phone": "563-3566"
}
}
In strict JSON:
- All string values must be in double quotes (single quotes won't do).
- The name portion of each name/value pair must also be in double quotes.
- The value of a property cannot be a function or method.
If your JSON data is not valid strict JSON, it will trip up certain JSON parsers, most notably, the JSON object of JavaScript. So make sure your data is valid syntax wise!
JSON data can be stored on the server as a text file for example, then retrieved using Ajax and converted into an actual JavaScript object for easy parsing. In general there are two ways to parse JSON:
- Use JavaScript's
eval()
function to convert the data into an actual JavaScript object. The advantage of this is that it works in older browsers (FF3 and below, IE7 and below, Opera 10 and below). The disadvantage is that it's slow and potentially unsafe unless you pre-screen the data for malicious code/ methods that could be brought to life usingeval()
. - Use JavaScript's built in
JSON
object. It's fast and safe. The only disadvantage is that it's only supported in newer browsers, such as FF3.5+, IE8+, and Opera 10.5+.
Native JSON object
The JSON object provides a native way to convert a JSON object into string, and a JSON string back into a JavaScript object. It is currently supported in FF3.5+, IE8+, and Opera 10.5+.
Methods
Note: "[]" surrounding a parameter below means the parameter is optional.
Methods | Description |
---|---|
stringify(obj, [replacer], [space]) | Converts a JavaScript object into a
JSON string. For example: var jsonobj={"name":"George",
"age":29, "friends":["John", "Sarah", "Albert"]} This method supports two optional parameters:
The following adds 10 white spaces to the beginning of each member inside the generated the JSON string: var jsonobj={"name":"George",
"age":29, "friends":["John", "Sarah", "Albert"]} More about the
|
parse(string, [reviver]) | Converts a JSON string into a
JavaScript object. For example: var jsonstr='{"name":"George",
"age":29, "friends":["John", "Sarah", "Albert"]}'
In the following example, I store today's date (in milliseconds) inside an object's property: var jobduty={"thedate":new Date().getTime(), "thetask": "Take out garbage"} When we convert this object to a JSON string then back
into a JSON object, the property " var jobstr=JSON.stringify(jobduty) Using a
var jobstr=JSON.stringify(jobduty) In this case I check to see if the property currently being worked on is "thedate", and if so, call new Date() on the property's value to return a date object representation of it. |
toJSON() | toJSON() dictates
how a JSON string will be serialized when JSON.stringify()
is called. If this method is defined within the JSON object, its
return value will be used by JSON.stringify() to form
the new JSON string.The below example defines a toJSON() method inside the JSON object we wish to serialize to sort any arrays within the object when it's converted into a JSON string: var company={} The toJSON() object also exists natively on the Date, Number, String, and Boolean objects. |
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words