使用 RestKit 映射空响应的对象 [英] Object Mapping an empty response with RestKit

查看:48
本文介绍了使用 RestKit 映射空响应的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢 此处的帮助和 RestKit邮件列表 我已经能够解析我的 JSON,但是现在我遇到了一个新问题,解析一个空响应.为了做好准备,当我的查询有结果时,JSON 如下所示:

Thanks to the help here and on the RestKit mailing list I've been able to parse my JSON, but now I have a new problem, parsing an empty response. To set the stage, here's what the JSON looks like when my query has results:

    {"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:blobMapping forKeyPath:@"blobsList.blobs"];

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

还有我的课程:

        @interface GetResponseInRegionResponse : NSObject
        {
            NSString* name;
            NSString* blobId;
            NSString* description;
            NSString* hint;
        }


        @interface GetResponseInRegionResponseList : NSObject
        {
            NSArray  *blobsList;
        }

但现在问题是我的服务器也可以为 JSON 返回这个:

But now the wrinkle is that my server can also return this for JSON:

        {"blobsList":""} 

是的,如果查询没有结果,我会返回.它使我的应用程序崩溃

Yeah, if the query has no results I get that back. It crashes my app

        restkit.object_mapping:RKObjectMapper.m:255 Performing object mapping sourceObject: {
            blobsList = "";
        }
         and targetObject: (null)

        2012-03-22 11:56:16.233 Ferret[7399:17a07] T restkit.object_mapping:RKObjectMapper.m:269 Examining keyPath 'blobs' for mappable content...
        2012-03-22 11:56:16.233 Ferret[7399:17a07] D restkit.object_mapping:RKObjectMapper.m:279 Found unmappable value at keyPath: blobs
        2012-03-22 11:56:16.233 Ferret[7399:17a07] T restkit.object_mapping:RKObjectMapper.m:269 Examining keyPath 'blobsList.blobs' for mappable content...
        2012-03-22 11:56:16.239 Ferret[7399:17a07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0xdb1d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key blobs.'

所以我试图想出一种方法让 ResKit 映射这个空响应.我已经尝试了所有这些:

So I'm trying to come up with a way to get ResKit to map this empty response. I've tried all of these:

[[RKObjectManager sharedManager].mappingProvider setMapping:blobListMapping forKeyPath:@"blobsList"];
[[RKObjectManager sharedManager].mappingProvider setMapping:NULL forKeyPath:@"blobsList"];
[blobListMapping mapKeyPath:@"" toAttribute:@"blobsList"];
[blobListMapping mapKeyPath:@"blobsList" toAttribute:@"blobsList"];

但它们都崩溃了.我正在尝试对源代码进行正面和反面处理,这个类不是关键 blob 的键值编码兼容"令人费解,因为那里没有任何 blob,只有 blobList.感谢您的帮助,谢谢!

But they all crash. I'm trying to make heads and tails of the source, the "this class is not key value coding-compliant for the key blobs" is puzzling since there aren't any blobs there, just blobsList. I appreciate any help, thanks!

推荐答案

我在我的项目中遇到了类似的问题.我正在向 Web 服务发帖,该服务将我的已发帖对象返回给我,该对象被包裹在一个数组中.这导致 RestKit 在解析时崩溃,因为它期望只返回单个对象而不是集合.为了解决这个问题,我实现了 objectLoader:willMapData: 委托方法并将响应对象从数组中拉出并通过 RestKit 解析发送该对象,一切都很好.

I ran into something similar in my project. I was posting to a web service and that service was returning to me my posted object wrapped in an array. This caused RestKit to crash when parsing because it expected to only get a single object back and not a collection. To fix the problem I implemented the objectLoader:willMapData: delegate method and pulled the response object out of the array and sent that object through the RestKit parsing and all is well.

也许你可以做一些类似的事情.在通过 RestKit 解析发送它之前,检查是否在 blobslist 中返回一个字符串并将其转换为空数组.

Maybe you can do something similar. Check to see if you get back a string in blobslist and convert that to an empty array before sending it through the RestKit parsing.

- (void)objectLoader:(RKObjectLoader *)objectLoader willMapData:(id *)mappableData
{
    // Horrible hack to convert posted object that gets returned as an array into a single object
    // that can be mapped.
    //
    if ([objectLoader.targetObject isKindOfClass:[TSLPostPostContract class]])
    {
        if ([*mappableData isKindOfClass:[NSArray class]])
        {
            *mappableData = [*mappableData objectAtIndex:0];
        }
    }
}

这篇关于使用 RestKit 映射空响应的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆