JSON (= JavaScript Object Notation), is a lightweight and fast mechanism to convert Javascript objects into a string and vice versa.
Since Javascripts objects consists of key/value
pairs its very easy to use and access JSON that way.
So if we have an object:
var myObj = {
foo: 'bar',
base: 'ball',
deep: {
java: 'script'
}
};
We can convert that into a string by calling window.JSON.stringify(myObj);
with the result of "{"foo":"bar","base":"ball","deep":{"java":"script"}}"
.
The other way around, we would call window.JSON.parse("a json string like the above");
.
JSON.parse()
returns a javascript object/array on success.
alert(myObj.deep.java); // 'script'
window.JSON
is not natively available in all browser. Some "older" browser need a little javascript plugin which offers the above mentioned functionality. Check http://www.json.org for further information.