ios - 如何在 RestKit 中映射 JSON 数组

标签 ios mapping restkit

我在将 JSON 数组映射到 RestKit 时遇到了一些麻烦。这是 JSON 文件的样子:

{"issuelist":[
    {
        "issue":[
            {
                "id":1,
                "beschreibung":"",
                "name":"Test1"
            },
            {
                "id":2,
                "beschreibung":"",
                "name":"Test2"
            }
        ]
    }
]}

我对“问题”数组感兴趣。这是我对单个问题的映射:

RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[self class] usingBlock:^(RKObjectMapping *mapping) {
        [mapping mapAttributes:@"name", @"beschreibung", nil];
        [mapping mapKeyPathsToAttributes:
                @"id", @"identifier",
                nil];
    }];

下面是我如何设置我的 ObjectMapping

RKObjectMappingProvider *omp = [RKObjectManager sharedManager].mappingProvider;

RKObjectMapping *issueMapping = [Issue mapping];
[omp addObjectMapping:issueMapping];

[omp setObjectMapping:issueMapping forKeyPath:@"issuelist.issue"];

不幸的是,这不起作用。我得到这样的日志输出:


    T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath 'name'. Transforming from type '__NSArrayI' to 'NSString'
    W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath 'name'. No strategy for transforming from '__NSArrayI' to 'NSString'
    T restkit.object_mapping:RKObjectMappingOperation.m:339 Skipped mapping of attribute value from keyPath 'name to keyPath 'name' -- value is unchanged ((null))
    T restkit.object_mapping:RKObjectMappingOperation.m:322 Mapping attribute value keyPath 'beschreibung' to 'beschreibung'
    T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath 'beschreibung'. Transforming from type '__NSArrayI' to 'NSString'
    W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath 'beschreibung'. No strategy for transforming from '__NSArrayI' to 'NSString'
    T restkit.object_mapping:RKObjectMappingOperation.m:339 Skipped mapping of attribute value from keyPath 'beschreibung to keyPath 'beschreibung' -- value is unchanged ((null))
    T restkit.object_mapping:RKObjectMappingOperation.m:322 Mapping attribute value keyPath 'id' to 'identifier'
    T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath 'id'. Transforming from type '__NSArrayI' to 'NSString'
    W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath 'id'. No strategy for transforming from '__NSArrayI' to 'NSString'
    T restkit.object_mapping:RKObjectMappingOperation.m:339 Skipped mapping of attribute value from keyPath 'id to keyPath 'identifier' -- value is unchanged ((null))
    D restkit.object_mapping:RKObjectMappingOperation.m:624 Finished mapping operation successfully...

似乎 RestKit 试图将整个数组映射到一个 Issue 中,而不是创建一个 Issue 数组。 我需要如何更改我的映射才能更正此问题?

感谢您的帮助!

最佳答案

试试这个:

RKObjectMapping* issueMapping = [RKObjectMapping mappingForClass: [Issue class] usingBlock:^(RKObjectMapping *mapping) {
    [mapping mapAttributes:@"name", @"beschreibung", nil];
    [mapping mapKeyPathsToAttributes:
     @"id", @"identifier",
     nil];
}];
issueMapping.rootKeyPath = @"issue";
[omp setObjectMaping: issueMapping forKeyPath: @"issuelist"];

这表示,当遇到问题列表键路径时使用问题映射。然后它说对于每个根本问题,创建一个问题对象。

关于ios - 如何在 RestKit 中映射 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11152133/

相关文章:

flash - 将任意绘图转换为交互式 map ?

java - 如何正确设置hibernate单向一对一映射

ios - ASIFormDataRequest POST 请求问题

ios - 从 iOS 应用程序移至 Safari 网页

iOS UITableView 单元格重复

heroku - 通过代理数据或通过 heroku 签署 url 从 S3 tiles 提供私有(private)映射?

ios - RKResponseDescriptor 与 RKRouter

ios - 通过 URL 中的 ID 映射 RestKit 关系

objective-c - RestKit 图片上传

ios - 在 UIButtons 上禁用 UIPanGestureRecognizer