我一直在很好地解析我的JSON,但我的服務器只是改變了我。我的JSON用於這個樣子:RestKit使用複雜對象數組映射
{
"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裏面?
我很感激任何幫助。謝謝!
當我這樣做,我得到:。 - ***因未捕獲異常'NSUnknownKeyException'而終止應用程序,原因:'[ valueForUndefinedKey:]:此類不是鍵編碼兼容的密鑰ID'。 –
2012-03-19 20:21:37
啊,那是我的錯誤,JSON有一個標記爲「id」的元素,我有一個名爲「blobId」的類成員而不是「id」。但我還需要有關係部分。我從RestKit郵件列表中取得了這個結果。如果那個人在這裏遇到這個問題的一部分,我會看看我是否可以分裂你們兩個人之間的恩賜。非常感謝! – 2012-03-19 20:48:03
您的JSON設置的方式不需要關係。在_object_「blobsList」裏面你直接有另一個_object_「blob」,只有你有這個數組。 「blobList」永遠不會包含數組,如果它真的像你的例子。 – k1th 2012-03-19 20:58:31