我正在尝试使用 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 属性为空之外,IAPClientMessage 中的属性都与预期相同。虽然有多个 IAPClientInfo 对象,但在 json 响应中,不清楚 IAPClientMessage 中的属性应该是什么(当前设置为 NSArray*)。有什么建议可以使这项工作正常进行吗?
德克萨斯州