如何在RestKit中映射JSON数组

tha*_*dor 5 mapping ios restkit

我有一些麻烦将JSON数组映射到RestKit.这就是JSON文件的样子:

{"issuelist":[
    {
        "issue":[
            {
                "id":1,
                "beschreibung":"",
                "name":"Test1"
            },
            {
                "id":2,
                "beschreibung":"",
                "name":"Test2"
            }
        ]
    }
]}
Run Code Online (Sandbox Code Playgroud)

我对"问题"数组很感兴趣.这是我对单个问题的映射:

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

以下是我设置ObjectMapping的方法

RKObjectMappingProvider *omp = [RKObjectManager sharedManager].mappingProvider;

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

[omp setObjectMapping:issueMapping forKeyPath:@"issuelist.issue"];
Run Code Online (Sandbox Code Playgroud)

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


    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试图在一个问题中映射整个arry而不是创建一系列问题.如何更改映射以更正此问题?

谢谢你的帮助!

Pau*_*nge 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"];
Run Code Online (Sandbox Code Playgroud)

这说,遇到issuelist keypath时使用issueMapping.然后它说出每个根问题,创建一个Issue对象.