用一组复杂对象进行RestKit映射
我一直在parsing我的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; }
当我parsing这个JSON的时候,我得到了一个有2个对象的JKArray对象,这两个对象都是JKDictionary对象。 所以清楚这是我的数据,但它是在JKDictionaryforms。 它从来没有映射到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中映射一个复杂对象的数组呢?
我感谢任何帮助。 谢谢!
你有没有试过只改变
[[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobsList.blobs"];
反映改变你的数据arrays的path?
接受的答案似乎是针对当前版本v0.2之前的一个版本。 我的解决scheme看起来有些不同,代码粘贴在下面:
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];