iPhone SDK: Parsing JSON data
So when developing for the iPhone with the official SDK, and trying to parse JSON data, there are a few options for JSON libraries to use:
- json-framework – General purpose JSON library for Objective-C, but contains a custom SDK that was built to be easily bundled with iPhone app projects.
- TouchJSON – Part of the TouchCode, a project of a bunch of classes that are targetted for iPhone development, so it’s deemed to be a very light and fast library.
- Maybe more? Let me know in the comments area.
Both are very under-documented (I mean, SERIOUSLY), but if you poke at them long enough, things will start making sense. I personally chose to use json-framework, as I initially had a few problems figuring out how to parse JSON content that had an array as the root object. Apparently TouchJSON officially only supports JSON results serialized as a object in the root level. As in:
{"values": ["value1", "value2", "value3"]}
Instead of:
["value1", "value2", "value3"]
That’s usually not a big deal, as you can just change the way you generate the JSON result in your server side code (if you are dealing with web services returning the JSON data), but what happens if you already have code expecting the server side to return that type of result?
Either way, I’m going to provide a simple usage example of json-framework to parse a JSON string. In order to install json-framework, you should follow the simple instructions available in its INSTALL file.
NSString *jsonString = @"[\"value1\", \"value2\", \"value3\"]"; SBJSON *json = [[SBJSON alloc] init]; NSError *error = nil; NSArray *results = [json objectWithString:jsonString error:&error]; [json release];
For my particular application, I was fetching the JSON string from a web service, and it was coming up as a NSData object, so I had to do the following:
NSString *jsonString = [[NSString alloc] initWithData:resourceData encoding:NSUTF8StringEncoding]; SBJSON *json = [[SBJSON alloc] init]; NSError *error = nil; NSArray *results = [json objectWithString:jsonString error:&error]; [json release]; [jsonString release];
Hopefully this is useful to someone else.



Mathieu said,
December 30, 2008 @ 8:34 am
Hello,
I read you message on: http://stackoverflow.com/questions/288412/deserializing-a-complex-json-result-array-of-dictionaries-with-touchjson
I need to parse the following JSON response: http://pastie.org/348983 and especially get the “name” parameter. I tried with TouchJSON and with the JSON-Framework you’re talking about, and it seems I cannot parse the inside of the “locations” part.
Can you help me? There’s not good example or documentation on the web for that.
Thank you so much for your help!
Mathieu said,
December 30, 2008 @ 10:43 am
I don’t see the comment I submitted earlier today. Maybe I missed a confirmation step?
jpm said,
December 30, 2008 @ 11:12 am
Mathieu,
Sorry, your comment was still set to be approved.
It’s actually pretty simple to decode JSON output, once you get the hang of it. You are totally right that it is very difficult to find documentation for any of this, which is why I wanted to write this post.
In any case, since your JSON string has a dictionary as the outermost object, you need to tweak my original code sample a bit to make it work.
That should work just fine (not tested though).
–Joao
Mathieu said,
December 30, 2008 @ 7:38 pm
Joao,
Thank you so much. It is working perfectly now. And I can just keep using TouchJSON only.
Thanks!
Matt said,
January 7, 2009 @ 8:12 pm
Can you help guide me on how to retrieve the data from a URL?
Thanks!
Rajendra said,
January 9, 2009 @ 1:33 am
I had write the same code in my application but it shown me data in markup tag i.e. means >, etc…
Please give me response if you have any solution for the same….
Thanks in advance.
Gerald said,
March 10, 2009 @ 9:03 pm
Thanks for listing TouchJSON… Previously I was using BSJSON (as linked from json.org), but Touch is much much faster!
Bahar said,
June 11, 2009 @ 12:58 am
I have developed a web service, which outputs JSON. So far I was using json-framework in fetching the web service in iphone. TouchJSON is much faster.
Thanks for sharing it.