【发布时间】:2012-04-02 08:14:58
【问题描述】:
我已经很好地解析了我的 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 中映射一个复杂对象的数组?
感谢您的帮助。谢谢!
【问题讨论】: