Native JSON Parsing

Introduction

 
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 I 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:
 
Native JSON Structure
 

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
  1. var viewSummary = {  
  2.     "pageId""archieveview",  
  3.     "url""jaiswalabhishek.blogspot.in",  
  4.     "content""Structure content",  
  5.     "viewTitle""GoogleSys...",  
  6.     "transition""slide"  
  7. }  
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
  1. 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:
 
json Serialization
 

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 ay script, not just JSON2.
 
Example | JSON2 | Load Function
  1. function loadScript(id, url, callback) {  
  2.  if (!document.getElementById(id)) {  
  3.   var script = document.createElement("script");  
  4.   script.type = "text/javascript";  
  5.    script.id = id;  
  6.    
  7.     if (script.readyState) {  
  8.         script.onreadystatechange = function () {  
  9.    
  10.             if (script.readystate === "loaded" || script.readyState === "complete") {  
  11.                 screen.onreadystatechange = null;  
  12.                 callback();  
  13.             }  
  14.         };  
  15.     }  
  16.    
  17.     else {  
  18.         script.onload = function () {  
  19.             callback();  
  20.         };  
  21.     }  
  22.     script.src = url;  
  23.     document.body.appendChild(script);  
  24.  }  
  25.    
  26.  else {  
  27.     callback();  
  28.     }  
  29.  }; 


Similar Articles