Json parsing vs xml parsing?

What are the advantage and disadvantage of json parsing? Why developers prefer to use json over xml parsing?

Answers


Json...

has a smaller overhead comparred to XML - XML's closing tags increase boilerplate code size by a factor of about 2. It's also more human-readable - consider

{
  "key": "value",
  "other key": 3.1415926535897932,
  "arr": [
    1,
    2,
    3
  ]
}

versus

<?xml version="1.0"?>
<!-- not to mention the DTD declaration -->
<myXmlFormat>
  <key>value</key>
  <otherKey>3.1415926535897932</otherKey>
  <arr>
    <number>1</number>
    <number>2</number>
    <number>3</number>
  </arr>
</myXmlFormat>

Is easier and quicker to parse (as it's more lightweight) - there are a lot of JSON parsers which are themselves smaller and quicker than their XML parser counterparts in the same programming language (consider libxml2 versus js0n).

JSON's data types also have a 1:1 mapping to data types traditionally considered "primitive" - such as strings, intergal and real numbers, arrays ans key-value tables. Furthermore, these primitive data types are easy to use with Foundation aa they're built into it - the above JSON can effortlessly parsed to an NSDictionary containing "key", "other key" and "arr" as keys, which correspond to the string "value", pi as an NSNumber and an NSArray, respoectively. Meanwhile XML has to be thought of additionally - maybe you have to even create custom classes to represent your own data structure described in the XML.

Specifically for the iOS platform: Cocoa's native XML parser, NSXMLParser is more than counterintuitive to use. The Foundation framework on iOS doesn't include the NSXMLNode class from Foundation on Mac OS X, and this forces developers to write their own spaghetto code to wrap all the XML to some structured data... well... structure instead of being able to use the included ones.


Need Your Help

Allowing gframe to grow with scrollbars inside window

r gtk gwidgets

I'd like to create a variable number of widgets inside a frame, based on data imported by the user, while keeping the frame from growing the window off the screen. If the frame becomes larger than ...

Is it expensive to re-create arrayList elements?

java optimization arraylist

I have managed to get myself into a situation where I cant re-use arrayList elements in my game.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.