我一直在很好地解析我的JSON,但我的服务器只是改变了我。我的JSON过去看起来像这样:
{
"blobs": [
{
"createdOn": "2012-03-16T15:13:12.551Z",
"description": "Fake description",
"hint": "And a useless hint",
"id": 400,
"name": "Fake CA one",
"publicId": "FF6",
"type": 0
},
{
"createdOn": "2012-03-16T17:33:48.514Z",
"description": "No hint on this one, but it does have a description.",
"hint": "Hint",
"id": 402,
"name": "Second fake one in CA",
"publicId": "FF8",
"type": 0
}
]
}
我的映射看起来像这样:
RKObjectMapping* blobMapping = [RKObjectMapping mappingForClass:[GetResponseInRegionResponse class]];
[blobMapping mapKeyPath:@"name" toAttribute:@"name"];
[blobMapping mapKeyPath:@"id" toAttribute:@"blobId"];
[blobMapping mapKeyPath:@"description" toAttribute:@"description"];
[blobMapping mapKeyPath:@"hint" toAttribute:@"hint"];
[[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobs"];
现在我的服务器已经改变了,我得到了回复:
{
"blobsList": {
"blobs": [
{
"createdOn" :"2012-03-16T15:13:12.551Z",
"description": "Fake description",
"hint": "And a useless hint",
"id": 400,
"name": "Fake CA one",
"publicId": "FF6",
"type": 0
},
{
"createdOn": "2012-03-16T17:33:48.514Z",
"description": "No hint on this one, but it does have a description.",
"hint": "Hint",
"id": 402,
"name": "Second fake one in CA",
"publicId": "FF8",
"type": 0
}
]
}
}
所以我把它添加到我的映射中:
RKObjectMapping* blobsListMapping = [RKObjectMapping mappingForClass:[GetResponseInRegionResponseList class]];
[blobsListMapping mapKeyPath:@"blobsList" toAttribute:@"blobsList"];
[[RKObjectManager sharedManager].mappingProvider setMapping:blobsListMapping forKeyPath:@"blobsList"];
这是我的课程:
@interface GetResponseInRegionResponse : NSObject
{
NSString* name;
NSString* blobId;
NSString* description;
NSString* hint;
}
@interface GetResponseInRegionResponseList : NSObject
{
NSArray *blobsList;
}
当我解析这个JSON时,我得到一个包含2个对象的JKArray的对象,这两个对象都是JKDictionary对象。很明显这是我的数据,但它是JKDictionary形式。它从未映射到GetResponseInRegionResponse类!
从阅读github文档看起来我想对数组使用toRelationship方法,但我只是没有看到它放在哪里。如果我按照“文章”示例试试这个:
[blobListMapping mapKeyPath:@"blobs" toAttribute:@"blobsList"];
[blobListMapping mapKeyPath:@"blobs" toRelationship:@"blobsList" withMapping:blobMapping];
我得到了这个例外:
2012-03-19 14:59:53.704 Ferret[8933:16303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to add mapping for keyPath blobsList, one already exists...'
那么如何在JSON中映射复杂对象的数组?
我感谢任何帮助。谢谢!
答案 0 :(得分:3)
接受的答案似乎是针对当前版本v0.2之前的版本。我的解决方案看起来略有不同,代码粘贴在下面:
RKObjectMapping * eventMapping = [RKObjectMapping mappingForClass:[RKJMEvent class]]; [eventMapping addAttributeMappingsFromDictionary:[RKJMEvent map]];
RKObjectMapping * eventResponseMapping =[RKObjectMapping mappingForClass:[RKJMEventsResponse class]];
[eventResponseMapping addAttributeMappingsFromDictionary:[RKJMEventsResponse map]];
[eventResponseMapping addRelationshipMappingWithSourceKeyPath:@"events" mapping:eventMapping];
RKResponseDescriptor *responseDescriptorEventResponse = [RKResponseDescriptor responseDescriptorWithMapping:eventResponseMapping
pathPattern:API_GET_EVENTS_PATH
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptorEventResponse];
答案 1 :(得分:2)
您是否尝试过只更改
[[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobsList.blobs"];
反映数据数组的更改路径?