altinkonlineConcrete5 Xcode

Attachments

XcodeProject.zip

How to parse JSON in iOS5


A while ago i've written the tutorial Xcode and parsing JSON. The code we've created there are we using here again. But we've added an new function wich can parse the JSON in iOS5 without any extra frameworks.

In this tutorial you would learn to parse JSON in iOS5.

To start with this tutorial I recommend you to read my first tutorial to understand the code. See the link before. 

Now we would make some changes to add the JSON parse method of iOS5. Open the 'RootViewController.h' and change it with the code below. Add two new methods to parse the JSON.

#import <UIKit/UIKit.h>
 
@interface RootViewController : UITableViewController {
 
    NSArray *tableData;
}
 
@property (nonatomic, retain) NSArray *tableData;
 
- (void)parseJSONIOS5;
- (void)parseJSON;
 
@end

Now it's time to add those methods to your 'RootViewController.m'. Add the following piece of code to your 'RootViewController.m'.

- (void)parseJSONIOS5 {
    // Download JSON
    NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://altinkonline.nl/temp/xcode-and-parsing-json.json"]];
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
 
    NSArray *items = [json objectForKey:@"items"];
    [self setTableData:items];
}
 
- (void) parseJSON {
    // Download JSON
    NSString *jsonString = [NSString 
                            stringWithContentsOfURL:[NSURL URLWithString:@"http://altinkonline.nl/temp/xcode-and-parsing-json.json"] 
                            encoding:NSStringEncodingConversionAllowLossy 
                            error:nil];
    // Create parser
    SBJSON *parser = [[SBJSON alloc] init];
	NSDictionary *results = [parser objectWithString:jsonString error:nil];
	[parser release], parser = nil;
    // Set tableData
    [self setTableData:[results objectForKey:@"items"]];
}

It's time to look at the method parseJSONIOS5. Here's coded the function to parse JSON in iOS5. First download the JSON as an NSData object. After downloading we would create an JSONobject with the data. This is done with the usage of the NSJSONSerializationclass. This NSJSONSerialization class is added in iOS5 and therefore not avialable in previous versions than iOS5

Now we could change the way to start our application. Change the method viewWillAppear: in your 'RootViewController.m' with the code below.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self parseJSONIOS5];
    //[self parseJSON];
}

Now you're parsing JSON by using the NSJSONSerialization class of iOS5. You could also check wich version of iOS is running and perform the right selector when the version is lower than iOS5.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
        [self parseJSONIOS5];
    else
        [self parseJSON];
}

You're done! You've succesfully created an method to parse JSON data in iOS5. While creating this method you've used the NSJSONSerialization class.