With iOS 5 SDK, JSON parsing has become very easy with NSJSONSerialization class.  Whenever I search for a programming tutorial on internet, I have a hard time. The problem with programming tutorials on internet is that they waste half of your time by walkthroughs of creating User Interface that you might not need or making code extra long by hooking up methods with UI elements or creating separate classes and a lot of variables.  Here is a very very simple tutorial that explains how JSON parsing in Objective C works using NSJSONSerialization.

Let’s say if we want to NSLog the statuses from my Twitter timeline (@jadoon88). Here is how you can request twitter timeline using JSON and then parse it to list the statuses:

Step 1: Prepare URL request to get the user timeline.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=jadoon88"]];

Step 2: Perform request and get JSON as a NSData object

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

Step 3: Parse the retrieved JSON to an NSArray

NSError *jsonParsingError = nil;
NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

Step 4: Creatie an NSDictionary object to to store contents of any single tweet (a single JSON Object).

NSDictionary *tweet;

Step 5: Iterate through publicTimeline NSArray to retrieve NSDictionary objects. The following code NSLogs the values with name ‘text’ in an object (That are Twitter status texts, have a look at GET SEARCH in Twitter API Resources).

for(int i=0; i<[publicTimeline count];i++)
tweet= [publicTimeline objectAtIndex:i];
NSLog(@”Statuses: %@”, [tweet objectForKey:@"text"]);

That is all! As easy as a pie.