If you are barely familiar with JSON then please go and have a look at my article:
Introduction to JSON
In that article you will become familiar with some basics of the JavaScript Object Notation (JSON).
Now am starting this, how to use native JSON parsing effectively for the best performance and effective results of our development work.
Description | Native JSON
JSON is a collection of name and value pairs based on a JavaScript object's associative array structure. This format is easy for any language to parse.
Structure | Native JSON
There are two structures used in Native JSON Parsing:
Array
An array is a list of objects that represent a value structure or we can say a collection of similar data types.
Objects
Objects are represented by a name value pair and can contain a nested object.
Example | JavaScript Object
var viewSummary = {
"pageId": "archieveview",
"url": "jaiswalabhishek.blogspot.in",
"content": "Structure content",
"viewTitle": "GoogleSys...",
"transition": "slide"
}
In this example each property contains a name and value. It is the best method to encapsulate both the name and value in “” to avoid reserved name collisions and to ensure proper data formatting.
Example | String Representation
var viesSummaryStr = "{ "pageId" : "archieveview","url" : "jaiswalabhishek.blogspot.in","content" : "Structure content","viewTitle" : "GoogleSys...","transition" : "slide" }"
Serialization | JSON
At the beginning there was no serialization mechanism in JSON, but later on this mechanism was developed.
There is a pair of serialization functions available in the functioning mechanism of JSON:
Parse() Function
The Parse() function converts a JSON string into a JavaScript structure (object/array).
Stringify() Function
The stringify() function converts a JavaScript object or array to a JSON string.
It is just the opposite of the parse() function.
Example | JSON Serialization Functions
Parse() Function
var str = JSON.parse(viewSummary);
Stringify() Function
var str = JSON.stringify(viewSummary);
JSON2 | Native JSON
If a web browser needs to support JSON without native parsing it can dynamically load a JSON2 library. The load function loads the script if they are not present and executes the callback process presents as an option in the development.
It can be used to load any script, not just JSON2.
Example | JSON2 | Load Function
function loadScript(id, url, callback) {
if (!document.getElementById(id)) {
var script = document.createElement("script");
script.type = "text/javascript";
script.id = id;
if (script.readyState) {
script.onreadystatechange = function () {
if (script.readystate === "loaded" || script.readyState === "complete") {
screen.onreadystatechange = null;
callback();
}
};
}
else {
script.onload = function () {
callback();
};
}
script.src = url;
document.body.appendChild(script);
}
else {
callback();
}
};