使用Restkit解析数组中未命名的JSON数组
我有以下JSON正文,其中包含数组中的十进制数数组: 一, 我已经创建了一个类DecimalEntity,它的助手属性号为NSDecimalNumber,递归关系号为1-many关系。这种变体也可能: 二, 如何设置DecimalEntity的映射,以便它解析上述JSON主体?我已经为number设置了属性映射,该映射适用于II。但它不适用于I。这就是为什么我使用 并将此decimalEntityPropertyMapping添加回decimalEntityMapping以创建递归关系:使用Restkit解析数组中未命名的JSON数组,json,restkit,key-value-coding,Json,Restkit,Key Value Coding,我有以下JSON正文,其中包含数组中的十进制数数组: 一, 我已经创建了一个类DecimalEntity,它的助手属性号为NSDecimalNumber,递归关系号为1-many关系。这种变体也可能: 二, 如何设置DecimalEntity的映射,以便它解析上述JSON主体?我已经为number设置了属性映射,该映射适用于II。但它不适用于I。这就是为什么我使用 并将此decimalEntityPropertyMapping添加回decimalEntityMapping以创建递归关系: [de
[decimalEntityMapping addPropertyMapping:decimalEntityPropertyMapping];
但此变体会崩溃**[NSNull hasPrefix:]:无法识别的选择器发送到实例**
我还尝试使用@unionofarray展平阵列,但我不知道如何使用它。
[映射addAttributeMappingsFromDictionary:@{@UnionOfArray:@numbers}]
谢谢你的提示
更新:
我试着用一种传统的方式来做,因为我认为它应该会起作用。我已经更改了我的类NSManagedObject子类的结构和关系:
十进制数组:
包含1-M到小数点的辅助关系:小数
决定性:
包含Decimal:number类型的帮助器属性
类DecimalEntityArray映射到外括号,而DecimalEntityArray映射到内括号:
"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:]而崩溃:无法识别的选择器被发送到实例和建议
将关键路径“arrayOfArrays”处的一对多关系值映射到“arrayOfArrays”。。。
警告:检测到包含另一个集合的集合的关系映射。这可能不是你想要的。考虑使用KVC集合运算符,如@ UNIONFORACLE来压缩可映射的集合。
这可能不是你想要的。但这才是我真正想要的。那么这种传统的方法不起作用了?我想我离正确还有一步
[NSNull hasPrefix:]:发送到实例的无法识别的选择器源自decimalEntityPropertyMapping的定义,其中KeyPath==nil。奇怪的是,根据文档,允许使用nil值:
sourceKeyPath:
从中检索源对象表示中要映射为关系的数据的关键路径。如果为nil,则直接针对父对象表示执行映射 解决方案
我已经检查了调试器添加符号异常的问题-[NSObject DoesNotReconficateSelector:]并在[keyPath isKindOfClass:[NSNull class]]返回YES时添加了一行;在RKMappingOperation.m中,它可以与上面定义的映射顺利工作:
// 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;
}
您可以控制源JSON吗?你看过使用动态映射吗?谢谢你,韦恩。不,我无法更改发送给客户端的方式。虽然我尽了最大努力推动更改,因为每个客户端都必须在此时进行某种欺骗来解析它,但api更改的可能性非常小。我正在考虑使用一个代理对象来解析主机数组,它将包含一个1-M到DecimalNumber的关系/或一个包含DecimalNumber的新类。我将检查动态映射动态映射应该有帮助,当您找到数组数组时,可能还会使用@UnionOfArray之类的收集运算符。我已经用我的rogue解决方案更新了这个问题。它起作用了。有趣的是没有副作用。如何添加一个新的meta属性@self,它将引导映射引擎直接将未命名的json字段映射到relationshop?添加您的解决方案作为答案,以便您可以接受它。@self问题最好作为github问题来问。
"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];
// 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;
}