RestKit使用一组复杂对象进行映射 [英] RestKit Mapping with an array of complex objects
问题描述
我一直在很好地解析我的JSON,但我的服务器只是改变了我。我的JSON过去看起来像这样:
I had been parsing my JSON quite nicely but my server just changed on me. My JSON used to look like this:
{
"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
}
]
}
我的映射看起来像这样:
and my mapping looked like this:
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"];
现在我的服务器已更改,我收到了回复:
Now my server has changed and I get this back:
{
"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
}
]
}
}
所以我添加了这个到我的映射:
So I added this to my mapping:
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时,我得到一个JKArray为2的对象其中的对象,都是JKDictionary对象。很明显这是我的数据,但它是以JKDictionary形式。它从未映射到GetResponseInRegionResponse类!
When I parse this JSON, I get one object that has a JKArray of 2 objects in it, both of those are JKDictionary objects. So clearly that is my data, but it is in JKDictionary form. It never mapped to the GetResponseInRegionResponse class!
从阅读github文档看起来我想对数组使用toRelationship方法,但我只是没有看到它放在哪里。如果我按照文章示例并尝试这样:
From reading the github docs it looks like I want to use a toRelationship method for arrays, but I'm just not seeing where to put it. If I follow the "articles" example and try this:
[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中的对象?
So how can I map an array of complex objects inside my JSON?
我感谢任何帮助。谢谢!
I appreciate any help. Thanks!
推荐答案
你是否尝试过只改变
[[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobsList.blobs"];
以反映数据阵列的更改路径?
to reflect the changed path to your data array?
这篇关于RestKit使用一组复杂对象进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!