Working with JSON in iOS

Let’s say you have successfully made a HTTPS call somewhere and you’ve found JSON objects are returned. Well, iOS 5.0+ made it a whole lot easier working with them as Dictionary objects.

Here’s an example.

1. Let’s say you have a NSMutableString or something like that which contains the JSON data. The string is called fixJSONString, I know what kind of name is that, but hey, this is just an example to get you going.

In this example, we’re going to work with that good ol’ trusty UserObject again.

                NSData *jsonData = [fixJSONString dataUsingEncoding:NSUTF8StringEncoding];
                NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&parseError];
                
                if (jsonArray)
                {

                    NSMutableArray *userObjects = [[NSMutableArray alloc]init];
                    
                    for(NSDictionary *item in jsonArray) {

                        UserObject *userObject = [[UserObject alloc] init]; 

                        NSLog(@"Item: %@", item);
                        
                        for (id key in item) {
                            NSLog(@"key: %@, value: %@", key, [item objectForKey:key]);
                            if ([key isEqualToString:@"emailaddress"])
                            {
                                userObject.emailAddress = [item objectForKey:key];
                            } else if ([key isEqualToString:@"firstname"]) {
                                userObject.firstName = [item objectForKey:key];
                            }
                        }
                        
                        [userObjects addObject:userObject];
                        
                    }
                    
                    //let's say you built out your prior DataController to be able to support multiple objects coming in
                    //reference post https://ssbits.wordpress.com/2012/08/22/database-integration-with-an-app
                    DataController *dataController = [[DataController alloc]init];    
                    [dataController initDatabase];
                    [dataController insertUserObjects:userObjects];
                    
                } else {
                    NSLog(@"Error parsing JSON: %@", parseError);
                    NSLog(@"JSON is nil");
                }

2. That’s it, working with JSON objects just became a whole lot easier! Pre iOS 5.0, sorry.

Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: