Thursday, 23 October 2014

JSON BASICS


Features of JSON

  • It is an Java script object notation
  • JSON is Language independent
  • It is a self-descriptive and easy to understand


Comparison XML vs JSON

Much Like XML



  1. Both JSON and XML is plain text
  2. Both JSON and XML is "self-describing" (human readable)
  3. Both JSON and XML is hierarchical (values within values)
  4. Both JSON and XML can be fetched with an HttpRequest


Much Unlike XML




  1. JSON doesn't use end tag
  2. JSON is shorter
  3. JSON is quicker to read and write
  4. JSON can use arrays


Example of JSON and XML

Ex1:

<employees>
 <employee>  
<firstname>ravi</firstname>
<secondname> p</secondname>
</employee>
<employee>  
<firstname>shiva</firstname>
<secondname> m</secondname>
</employee>
</employees>


Ex 2:

JSOn is also same like xml but without end tag:
“employees”:[
 {“firstname”:”shiva”,”lastname”:”p”},
{“firstname”:”kishore”,”lastname”:”m”},
]

convert JSON string to java script object

You can convert JSON string to java script object in java script:

var jsonText=’{“employees”:[‘+
‘ {“firstname”:”shiva”,”lastname”:”p”},’+
‘{“firstname”:”kishore”,”lastname”:”m”},’+
‘]}’;

Var jsonobjet=JSON.parse(jsonText);


System.alert(jsonobjet. Employees[0].firstname+’return name of the first element’);

No comments:

Post a Comment