使用Restkit在数组中解析未命名的JSON数组

时间:2014-06-09 07:49:55

标签: json restkit key-value-coding

我有以下JSON主体,其中包含数组中的十进制数组:

(I。)

{
"decimals":
  [
     [
        18000.00,
        18000.00
     ]
  ],
} 

我创建了一个带有辅助属性" DecimalEntity"的班级number。 (NSDecimalNumber)和递归关系" numbers" (1多关系)这种变体也是可能的:

(II。)

 {
    "decimals":
         [
            18000.00,
            18000.00
         ],
    }

如何设置DecimalEntity的映射,以便解析上述JSON个体?我为" number"设置了属性映射。这适用于(II。)但它不适用于(I.)这就是为什么我创造了DecimalEntity和" numbers"之间的关系。使用

RKPropertyMapping* decimalEntityPropertyMapping =
                              [RKRelationshipMapping relationshipMappingFromKeyPath:nil
                                                                          toKeyPath:@"numbers"
                                                                        withMapping:decimalEntityMapping];

并将此decimalEntityPropertyMapping添加回decimalEntityMapping以创建递归关系:

[decimalEntityMapping                        addPropertyMapping:decimalEntityPropertyMapping];

但是此变体与**[NSNull hasPrefix:]:无法识别的选择器发送到实例**

崩溃

此外,我尝试使用@unionOfArrays展平数组,但我不知道如何使用它。     [mapping addAttributeMappingsFromDictionary:@{@"@unionOfArrays": @"numbers" }];

谢谢您的提示。

更新:

我试图以传统的方式做到这一点,因为我觉得它应该有效。我已经改变了我的类的结构和关系(NSManagedObject子类):

DecimalEntityArray:   包含一个辅助关系1-M到DecimalEntity'"十进制"

DecimalEntity:   包含Decimal类型的辅助属性:" number"

DecimalEntityArray类映射到外括号,DecimalEntity映射到内括号:

"arrayOfArrays":
  [ >>>>> 1-M, DecimalEntityArray
     [ >>>>> 1-M, DecimalEntity
        18000.00,
        18000.00
     ]
  ],

映射设置如下所示:

// setting up the attribute mappings:
RKObjectMapping* decimalEntityMapping               = 
    [DecimalEntity mappingForStore:managedObjectStore];
RKObjectMapping* decimalEntityArrayMapping          = 
    [DecimalEntityArray mappingForStore:managedObjectStore];

// the **nil** value denotes the fact that there is no dictionary, 
// just a direct mapping to NSSet of DecimalEntity's  
RKPropertyMapping* decimalEntityPropertyMapping =
              [RKRelationshipMapping relationshipMappingFromKeyPath:nil
                                                          toKeyPath:@"decimals"
                                                        withMapping:decimalEntityMapping];

// setup the relationship between DecimalEntityArray and its DecimalEntity's 
[decimalEntityArrayMapping   addPropertyMapping:decimalEntityPropertyMapping];


// finally setup the relation between the outermost json attribute, "arrayOfArrays" 
// and DecimalEntityArray
RKPropertyMapping* decimalEntityArrayPropertyMappingPotentialWin =
    [RKRelationshipMapping relationshipMappingFromKeyPath:@"potentionalWin"
                                                toKeyPath:@"potentionalWin"
                                              withMapping:decimalEntityArrayMapping];

但是这个解决方案崩溃了 [NSNull hasPrefix:]:发送到实例的无法识别的选择器和建议

在keyPath' arrayOfArrays'中映射一对多关系值到' arrayOfArrays' ... 警告:检测到包含其他集合的集合的关系映射。这可能不是你想要的。考虑使用KVC集合运算符(例如@unionOfArrays)来展平您的可映射集合。

这可能不是你想要的。但这才是我想要的。所以这种传统方式不起作用?我想我是正确的一步。

[NSNull hasPrefix:]:发送到实例的无法识别的选择器源自decimalEntityPropertyMapping的定义,其中KeyPath == nil。奇怪的是根据文档允许nil值:

sourceKeyPath:
从中检索要映射为关系的源对象表示中的数据的关键路径。如果nil,则直接针对父对象表示执行映射。

1 个答案:

答案 0 :(得分:0)

我已经用调试器检查了这个问题(用符号 - [NSObject doesNotRecognizeSelector:] 添加了符号异常)并添加了行 if([keyPath isKindOfClass:[NSNull class]])在 RKMappingOperation.m 中返回YES; ,它可以顺利运行以上定义的映射:

// Returns YES if there is a value present for at least one key path in the given collection
static BOOL RKObjectContainsValueForKeyPaths(id representation, NSArray *keyPaths)
{
    for (NSString *keyPath in keyPaths) {
        if([keyPath isKindOfClass:[NSNull class]]) return YES;
        if ([representation valueForKeyPath:keyPath]) return YES;
    }
    return NO;
}