我正在尝试使用RestKit 0.20处理包含嵌套字典的休息响应。这是我正在尝试处理的json响应:
{
"name": "Tom",
"msgTo": "Hi ",
"msgFrom": "Hey",
"uuid": "4",
"ClientInfo": {
"Tom": {
"AttributeA": "0",
"AttributeB": "28"
},
"Sam": {
"AttributeA": "10",
"AttributeB": "28"
}
}
}
问题出在ClientInfo对象周围。该对象是使用pojo jackson序列化从服务器发布的地图[Map>]的序列化Java地图。
这是iOS / RestKit中我用来处理响应的2个模型:
@interface IAPClientInfo : NSObject
@property (weak, nonatomic) NSString * mapName;
@property (weak,nonatomic) NSString *propertyCount;
@property (weak,nonatomic) NSString *affinityValue;
@end
@interface IAPClientMessage : NSObject
@property (weak, nonatomic) NSString * name;
@property (weak, nonatomic) NSString * msgTo;
@property (weak, nonatomic) NSString * msgFrom;
@property (weak, nonatomic) NSString * uuid;
// should this be an array?
@property (weak, nonatomic) NSArray * iapClientInfoArray;
@end
这是代码
// inner mapping
RKObjectMapping *clientInfoMapping = [RKObjectMapping mappingForClass:[IAPClientInfo class]];
[clientInfoMapping setForceCollectionMapping:YES];
[clientInfoMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"mapName"];
[clientInfoMapping addAttributeMappingsFromDictionary:@{@"(mapName).AttributeA" : @"propertyCount", @"(mapName).AttributeB" : @"affinityValue"}];
// outer mapping
RKObjectMapping *messageMapping = [RKObjectMapping mappingForClass:[IAPClientMessage class]];
[messageMapping addAttributeMappingsFromDictionary:@{@"msgFrom" : @"msgFrom",@"msgTo" : @"msgTo",@"uuid" : @"uuid",@"name" : @"name"}];
// relationship b/w inner and outer
RKRelationshipMapping *relationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"ClientInfo" toKeyPath:@"iapClientInfoArray" withMapping:clientInfoMapping];
[messageMapping addPropertyMapping:relationshipMapping];
// response
RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:messageMapping
pathPattern:nil
keyPath:@""
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
处理响应时,除了iapClientInfoArray属性(为null)之外,IAPClientMessage中的属性都是预期的。虽然有多个IAPClientInfo对象,但在json响应中,不清楚IAPClientMessage中的属性应该是什么(当前设置为NSArray *)。有任何建议使这项工作正常吗?
TX。
答案 0 :(得分:0)
我通过将所有属性从“弱”改为“强”来实现这一点。
首先,我根据您的建议启动了日志记录
RKLogConfigureByName(“RestKit / ObjectMapping”,RKLogLevelTrace);
它显示正确填充了IAPClientInfo和IAPClientMessage对象,表明基本设置没有问题。
IAPClientInfo对象正在正确填充但是为空,因此我怀疑存在内存管理问题。
阅读本文
http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
澄清了所需的用法 - 我需要所有属性“拥有”他们所指向的内容。因此,我需要他们使用'强'而不是'弱'语义。我正在使用ARC。一旦'创建'代码超出范围,'弱'语义会使指针(我怀疑)指向'nil'。
一旦以这种方式改变了属性,一切都按预期工作 - iapClientInfoArray *指向正确构造的IAPClientInfo实例的数组。
感谢。