Main Difference between JSON and XML
Major difference between JavaScript object notation (JSON) and Extensible Markup language XML,JSON extended from JavaScript and developed by Douglas, XML extended from “Standard Generalized Markup Language and developed by W3C.
JSON
XML
JavaScript object Notation
Extensible Markup language.
Doesn't support for Namespace
Support for Namespace
Doesn't support for comments
Support for comments
Cant convert to another format
Can convert to another format using XSLT
Its light-weighted because serialized format and so faster compare to XML
It's not light-weighted Compare to JSON because its can have more character for same using in JSON
Its support data type include int,string and array also
It's doesn't support for data type ,so we go for parsed into particular data type.
Its text-based format
Its text-based format but having some set rules for encoding
Its better Usage for web service
Its better Usage for web configuration
Its Data oriented and easily mapped
Its Document oriented and not that much easily to mapping
No need to use start and end tag
Need to use start and end tag
It's easier to parse from JavaScript
By using namespace we can implement for JavaScript
The JSON.parse() method parses a string as JSON, optionally transforming the value produced by parsing.
XmlDoc=parser.parseFromString(txt,"text/xml");
Syntax
JSON.parse(text[,reviver])
Text:
string to parse as JSON
reviver:
prescribes how the value originally produced by parsing is transformed, before being returned
sample :
JSON.parse(‘true');
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null');
Sample Code JSON :
{
"SalesPerson": [
{
"name": "Prabu",
"gender": "male">
“Age":23
},
{
"name": "Raju",
"gender": "male"
“Age":26
} ]
}
Syntax
xmlDoc=parser.parseFromString(txt,"text/xml");
Sample Code XML:
//< SalesPerson >
// < SalesPerson >
//<'name>prabu
//
//
//<\ SalesPerson >
/// < SalesPerson >
///<'name>raju
// <'gender>male
//
/// <\ SalesPerson >
// <\ SalesPerson >
Its not such a interface for getting direct access to a part in JSON data-structure
In XML, Using XPath, it is possible to get the direct access to a particular part of multiple parts of XML data- structure.
Using above xml sample code :
var document = new XPathDocument(@"sample.xml");
var navigator = document.CreateNavigator();
var books = navigator.Select("/SalesPerson/SalesPerson/name");