我在iOS上使用RestKit从kraken.com中检索一些数据
我有以下JSON数据:
{
"error":[],
"result":{
"XXBTZUSD":{
"a":["475.00000","1"],
"b":["465.00639","2"],
"c":["475.00000","0.01000389"],
"v":["1.01078477","1.01078477"],
"p":["475.00008","475.00008"],
"t":[6,6],
"l":["475.00000","475.00000"],
"h":["475.00013","475.00013"],
"o":"475.00001"
}
}
}
我的keyPath是@"result.XXBTZUSD"
,我对每个属性中数组的第一个成员感兴趣,即'a'
,'b'
,'c'
等等除'o'
之外的其他值,因为它的值不是数组。需要明确的是,我感兴趣的示例值"475.00000"
为'a'
。我正在尝试将这些值映射到我自己的数据模型中的NSNumber
属性。
除了: 我会动态地这样做,因为我也从其他来源获得类似的数据,但具有不同的结构化数据。
另外,我不相信我可以使用RKObjectMappingMatcher
因为我没有预期价值"对于这些数据。
修改
我继续处理RKDynamicMapping
和RKBlockValueTransformer
逻辑:
RKDynamicMapping *dynamicMapping = [RKDynamicMapping new];
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation)
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:class];
if ([url.absoluteString isEqualToString:@"https://api.kraken.com"])
{
RKBlockValueTransformer *arrayToNumberValueTransformer = [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class inputValueClass, __unsafe_unretained Class outputValueClass) {
BOOL isTransformableValue = NO;
if ([inputValueClass isSubclassOfClass:[NSArray class]] && [outputValueClass isSubclassOfClass:[NSNumber class]])
{
isTransformableValue = YES;
}
return isTransformableValue;
} transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, __unsafe_unretained Class outputClass, NSError *__autoreleasing *error) {
RKValueTransformerTestInputValueIsKindOfClass(inputValue, [NSArray class], error);
RKValueTransformerTestOutputValueClassIsSubclassOfClass(outputClass, [NSNumber class], error);
*outputValue = [[NSNumber alloc] initWithDouble:[[inputValue objectAtIndex:0] doubleValue]];
return YES;
}];
arrayToNumberValueTransformer.name = @"ArrayToNumberValueTransformer";
[[RKValueTransformer defaultValueTransformer] insertValueTransformer:arrayToNumberValueTransformer atIndex:0];
}
if (sourceKeyPath && [sourceKeyPath length] > 0)
{
// Special case (ignore):
if ([url.absoluteString isEqualToString:@"http://blockchain.info"])
{
[mapping addAttributeMappingsFromDictionary:@{sourceKeyPath:@"currencyCode"}];
}
//
// The following is useful if you're expecting a set but the service returns a single value
//[mapping setForceCollectionMapping:YES]; // RestKit cannot infer this information on its own
//
}
// Where a 'key' is the JSON representation and its value is the internal object representation
NSDictionary *attributeMappings = [query objectForKey:@"attributeMappings"];
[mapping addAttributeMappingsFromDictionary:attributeMappings];
return mapping;
}];
修改
我现在已经映射了数据但是我不确定我是否正确地将RKBlockValueTransformer
插入到默认值变换器列表中?如果我在0
插入,我会破坏其他默认值变换器的功能吗?