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
Much Like XML
- Both JSON and XML is plain text
- Both JSON and XML is "self-describing" (human readable)
- Both JSON and XML is hierarchical (values within values)
- Both JSON and XML can be fetched with an HttpRequest
Much Unlike XML
- JSON doesn't use end tag
- JSON is shorter
- JSON is quicker to read and write
- 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’);