Created
July 1, 2013 15:32
-
-
Save scottsappen/5901890 to your computer and use it in GitHub Desktop.
iOS 5.0+ made it a whole lot easier working with them as Dictionary objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 http://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"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment