5

我在将 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 在 keyPath 'name' 处找到可转换的值。从类型“__NSArrayI”转换为“NSString”
    W restkit.object_mapping:RKObjectMappingOperation.m:232 keyPath 'name' 处的值转换失败。没有从 '__NSArrayI' 转换为 'NSString' 的策略
    T restkit.object_mapping:RKObjectMappingOperation.m:339 跳过了从 keyPath 'name 到 keyPath 'name' 的属性值的映射——值不变((null))
    T restkit.object_mapping:RKObjectMappingOperation.m:322 映射属性值 keyPath 'beschreibung' 到 'beschreibung'
    T restkit.object_mapping:RKObjectMappingOperation.m:152 在 keyPath 'beschreibung' 找到可转换的值。从类型“__NSArrayI”转换为“NSString”
    W restkit.object_mapping:RKObjectMappingOperation.m:232 keyPath 'beschreibung' 处的值转换失败。没有从 '__NSArrayI' 转换为 'NSString' 的策略
    T restkit.object_mapping:RKObjectMappingOperation.m:339 跳过了从 keyPath 'beschreibung 到 keyPath 'beschreibung' 的属性值的映射——值不变((null))
    T restkit.object_mapping:RKObjectMappingOperation.m:322 映射属性值 keyPath 'id' 到 'identifier'
    T restkit.object_mapping:RKObjectMappingOperation.m:152 在 keyPath 'id' 找到可转换的值。从类型“__NSArrayI”转换为“NSString”
    W restkit.object_mapping:RKObjectMappingOperation.m:232 keyPath 'id' 处的值转换失败。没有从 '__NSArrayI' 转换为 'NSString' 的策略
    T restkit.object_mapping:RKObjectMappingOperation.m:339 跳过了从 keyPath 'id 到 keyPath 'identifier' 的属性值的映射——值不变((null))
    D restkit.object_mapping:RKObjectMappingOperation.m:624 映射操作成功...

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

谢谢你的帮助!

4

1 回答 1

9

试试这个:

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"];

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

于 2012-06-22T08:18:28.993 回答