Post Pic

How to use SBJSON and TouchJSON

We at ObjectGraph have mostly used SBJSON, An excellent library from Stig Brautaset which implements a strict JSON parser and generator in Objective-C. Recently we noticed others using TouchJSON and wanted to explore it to see if it offers any advantages over SBJSON.

There has been a good discussion on this subject on stackoverflow.com which can be read here.

http://stackoverflow.com/questions/286087/best-json-library-to-use-when-developing-an-iphone-application

I will show how to use each of these libraries to decode JSON code in this post.

Touch JSON:
Include the following in your header

#import "CJSONDeserializer.h"
NSString *twitterTrendsUrl=@"http://search.twitter.com/trends.json";
NSData *flickrData=
[NSData dataWithContentsOfURL:[NSURL URLWithString:twitterTrendsUrl]];
NSError *theError = nil;
id theObject =
 [[CJSONDeserializer deserializer] deserialize:flickrData error:&theError];
NSLog(@"Error: %@", theError);
NSLog(@"Result: %@", theObject);

SB JSON:

The thing i like about SBJSON is it adds functions directly into NSString to get NSDictionary or NSArray objects when deserializing JSON strings.

Start with including the following header

#import "JSON.h"

Then you can simply use the following code

NSString *twitterTrendsUrl=@"http://search.twitter.com/trends.json";
NSString *output=
[NSString stringWithContentsOfURL:[NSURL URLWithString:twitterTrendsUrl]];
id theObject=	[output JSONValue];
NSLog(@"%@",theObject);

Please remember stringWithContentsOfURL has been deprecated so I would not recommend using it. We also just include the entire source of the specific JSON library when building our apps.

Related posts:

  1. Encrypting / Decrypting / Base64 Encode / Decode in iPhone Objective-C I went through websites to find good tricks to send...
  2. Download an Image and Save it as PNG or JPEG in iPhone SDK Here is a quick example to save an image as...
  3. [iPhone Development] How to Parse HTML A few weeks ago, I was looking for a simple...
  4. iPhone SDK 3.0 – Playing with Map Kit – Part 3 This is the 3 part of me Playing with Map...
  5. iPhone SDK 3.0 – Playing with Game Kit – Part 1 My first series of articles explaining how to use Game...

Related posts brought to you by Yet Another Related Posts Plugin.

One Response

02.24.10

It is really good to share with us. keep it up. It really helpful to all Iphone developer

Leave Your Response

* Name, Email, Comment are Required