JSON parsing

August 23rd, 2011 posted in Web technology

Many people have written about parsing JSON data on the iPhone, but why use JSON and not XML.
Well this has to do with overhead, JSON is smaller in it’s mark-up then XML thus it will take less traffic on the cellular network.

But there are some drawback in using JSON, since there is no real strict way of checking the model. Like DTD is for XML.

So you will need to make strict …
When working with third party developers this is even greater chance of an error then when building all the parts your self.

Validating

I’ve you have ever worked with XML you might have used xmllint, xmllint is great way to valid XML.
Well some smart people at arc90 thought that this could also work with JSON, so they create jsonlint.
The smart thing is that it is webbased, there is no need to install anything.

JSONLint woks by just copy pasting in the in the JSON code, or entering the URL to the JSON output in the editor.
When you validate it will validate you JSON just like xmllint and report the first error in the JSON to you.

Most commonly made error, that is often the fault of some library, is that when there is only one item in an array the array [] are omitted. Thus not make it an array and you have to check this every time in you app.

Libraries

There are many libraries out there to parse XML or JSON, since I’m a iOS developer here are some of the one’s that I use.

AQXMLParser

Create by the great Jim Dovey. This nice lite parser solves some of the problems when reading large XML files. Keep the memory usage as low as posible.
By extending the AQXMLParserDelegate you can easly add methods to from XML Nodes key names, this is way better then
one big if statement.
Download

TouchXML

Small and nice little library that’s more DOM like. Should not be used for large XML you can run into memory error when loading in large XML files.
Best thing it support XPath.
Download

JSONKit

Speed kills they say, well not with this great library. It fast, really fast.
There really isn’t any need of an other JSON library.
Download