我在将 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/