Ios 使用复杂对象数组的RestKit映射

Ios 使用复杂对象数组的RestKit映射,ios,json,restkit,Ios,Json,Restkit,我一直在很好地解析JSON,但我的服务器刚刚发生了变化。我的JSON过去是这样的: { "blobs": [ { "createdOn": "2012-03-16T15:13:12.551Z", "description": "Fake description", "hint": "And a useless hint", "id": 400, "n

我一直在很好地解析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"];
我的地图是这样的:

{
    "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时,我得到一个包含两个对象的JKArray的对象,这两个对象都是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中映射复杂对象的数组


谢谢你的帮助。谢谢

您是否只尝试过改变

    [[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobsList.blobs"];

要反映更改后的数据数组路径?

接受的答案似乎是针对v0.2之前的版本,即当前版本。我的解决方案看起来略有不同,代码粘贴如下:


当我这样做时,我得到:。--***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:该类不符合密钥id的键值编码。“啊,这是我的错误,JSON有一个标记为“id”的元素,我有一个名为“blobId”而不是“id”的类成员。但我还需要加入toRelationship部分。我从RestKit邮件列表上查到的。如果那个人在这里插话说问题的那一部分,我看我能不能把赏金分给你们两个。非常感谢你!JSON的设置方式不需要关系。在对象“blobsList”中,直接有另一个对象“blobs”,只有在那里才有数组。“blobList”永远不会包含数组,如果它真的像您的示例中那样的话。是的,我需要进一步检查它。现在我的didLoadObject的对象是由3个而不是2个对象组成的数组,外部对象和2个内部对象。真奇怪。。。我明天试试。谢谢对我忘了提到你应该保留你原来的设置,只在上面添加一行-没有额外的关系…谢谢,但如果它使用类名和关键路径,从问题而不是“事件”等等,那就更清楚了@murrayc免费编辑我两年前的答案,如果你认为它更有用的话!