Created
February 17, 2014 03:35
JSON mapper examples
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
//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