I am confused completely to parse this kind of response in restkit. I am using this link but i am not able to understand how to parse this response.Any help would be appreciated.
“groups”: {
"group1": [
{
"email": "blake@restkit.org",
"favorite_animal": "Monkey"
},
{
"email": "slake@restkit.org",
"favorite_animal": "Donkey"
}
],
"group2": [
{
"email": "sarah@restkit.org",
"favorite_animal": "Cat"
},
{
"email": "varah@restkit.org",
"favorite_animal": "Cow"
}
]
}
I am Using Below mapping.
@interface GroupResponse : NSObject
@property (nonatomic, strong) NSArray *Groups;
+ (RKObjectMapping *)mapping;
@end
@implementation GroupResponse
+ (RKObjectMapping *)mapping {
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class]];
[objectMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@“groups” toKeyPath:@“Groups” withMapping:[GroupData mapping]]];
return objectMapping;
}
@end
@interface GroupData : NSObject
@property (nonatomic, strong) NSString *groupName;
@property (nonatomic, strong) NSString *arrPersons;
+ (RKObjectMapping *)mapping;
@end
@implementation GroupData
+ (RKObjectMapping *)mapping {
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class]];
objectMapping.forceCollectionMapping = YES;
[objectMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"groupName"];
[objectMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@“(groupName)” toKeyPath:@"arrPersons" withMapping:[Person mapping]]];
return objectMapping;
}
@end
@interface Person : NSObject
@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) NSString *favAnimal;
+ (RKObjectMapping *) mapping;
@end
@implementation Person
+ (RKObjectMapping *)mapping {
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class]];
[objectMapping addAttributeMappingsFromDictionary:@{@"email" : @"email",
@"favorite_animal" : @"favAnimal"}];
return objectMapping;
}
@end
Everytime arrPersons is nil. How to do proper mapping in this case.
This attribute:
@property (nonatomic, strong) NSString *arrPersons;
should actually be a mutable array, not a string type:
@property (nonatomic, strong) NSMutableArray *arrPersons;
because the nested JSON array can't be converted into a string and your mapping indicates that it should be processed into an array of Person
objects.