Skip to content

Instantly share code, notes, and snippets.

@alex-zige
Created February 17, 2014 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-zige/9044285 to your computer and use it in GitHub Desktop.
Save alex-zige/9044285 to your computer and use it in GitHub Desktop.
JSON mapper examples
//with custom Class
NSString *jsonFileName = @"resources";
NSString *filePath = [[NSBundle mainBundle] pathForResource:jsonFileName ofType:@"json"];
NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
ResourceList *resourceList = [ResourceList createObjectsUsingJSONDictwithCustomClass:jsonObject];
NSLog(@"resources %@", resourceList);
#with undefined Meetings returns NSArray
NSString *jsonFileName1 = @"meetings";
NSString *filePath1 = [[NSBundle mainBundle] pathForResource:jsonFileName1 ofType:@"json"];
NSData *JSONData1 = [NSData dataWithContentsOfFile:filePath1 options:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject1 = [NSJSONSerialization JSONObjectWithData:JSONData1 options:NSJSONReadingMutableContainers error:nil];
NSArray *mettings = [BaseModel createObjectsUsingJSONDict:jsonObject1];
NSLog(@"meetings %@", mettings);
#with Custom Class return instance with attributes
NSString *jsonFileName2 = @"meeting";
NSString *filePath2 = [[NSBundle mainBundle] pathForResource:jsonFileName2 ofType:@"json"];
NSData *JSONData2 = [NSData dataWithContentsOfFile:filePath2 options:NSDataReadingMappedIfSafe error:nil];
NSDictionary *jsonObject2 = [NSJSONSerialization JSONObjectWithData:JSONData2 options:NSJSONReadingMutableContainers error:nil];
Meeting *meeting = [Meeting createObjectsUsingJSONDictwithCustomClass:jsonObject2];
NSLog(@"single meeting %@",meeting);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment