Skip to content

Instantly share code, notes, and snippets.

@csmobile
Created July 1, 2014 17:48
Show Gist options
  • Save csmobile/55f49d767fb2460a2175 to your computer and use it in GitHub Desktop.
Save csmobile/55f49d767fb2460a2175 to your computer and use it in GitHub Desktop.
Parse JSON to object
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error];//response object is your response from server as NSData
if ([json isKindOfClass:[NSDictionary class]]){ //Added instrospection as suggested in comment.
NSArray *yourStaffDictionaryArray = json[@"directory"];
if ([yourStaffDictionaryArray isKindOfClass:[NSArray class]]){//Added instrospection as suggested in comment.
for (NSDictionary *dictionary in yourStaffDictionaryArray) {
Staff *staff = [[Staff alloc] init];
staff.id = [[dictionary objectForKey:@"id"] integerValue];
staff.fname = [dictionary objectForKey:@"fName"];
staff.lname = [dictionary objectForKey:@"lName"];
//Do this for all property
[yourArray addObject:staff];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment