Parse json feeds using jsonkit iOS

I am trying to use the JSONKIt found here https://github.com/johnezang/JSONKit to parse the JSON stream and put into objective-c objects. I'm new to iOS and don't know where to start. Are there any good tutorials to use this library?

+4
source share
2 answers

After googling, I did not find any tutorials, but using JSONKit should be self-evident.

After loading your JSON channel using NSURLConnection or ASIHTTPRequest, simply create a dictionary of all objects in the JSON channel as follows:

//jsonString is your downloaded string JSON Feed NSDictionary *deserializedData = [jsonString objectFromJSONString]; //Helpful snippet to log all the deserialized objects and their keys NSLog(@"%@", [deserializedData description]); 

After creating the dictionary, you can simply do something like this:

  NSString *string = [deserializedData objectForKey:@"someJSONKey"]; 

And these are the basics of JSONKit.

JSONKit is much more powerful, of course, you can find some other things you can do with it in JSONKit.h

+16
source

I would prefer to make the assumption that objectFromJSONString returns an NSDictionary , it can very well return an array or nil , especially if the server returns some rarely used and conceivable errors.

A more appropriate action would be the following:

 NSError *error; id rawData = [jsonString objectFromJSONStringWithParseOptions:JKParseOptionNone error:&error]; if ( error != nil ) { // evaluate the error and handle appropriately } if ( [rawData isKindOfClass:[NSDictionary class]] ) { // process dictionary } else if ( [rawData isKindOfClass:[NSArray class]] ) { // process array } else { // someting else happened, 'rawData' is likely 'nil' // handle appropriately } 

Without these checks, you may well encounter a run-time error because the server unexpectedly returned unexpectedly.

0
source

All Articles