I am using Restkit Version 0.2 to map JSON results to Objective-C Objects. The JSON received looks like this:
我使用Restkit版本0.2将JSON结果映射到Objective-C对象。收到的JSON看起来像这样:
{
"result": {
"errorCode": 0,
"errorMsg": "ok",
"data": {
"orderitems": [
{
"id": "46",
"o_order_id": "15",
"p_product_id": "7",
"t_event_id": "1",
"quantity": "1",
"price": "4.5",
"name": "Name1",
"unit": "something1",
"image": "images/7.png"
},
{
"id": "47",
"o_order_id": "15",
"p_product_id": "10",
"t_event_id": "1",
"quantity": "1",
"price": "3.99",
"name": "Name2",
"unit": "something2",
"image": "images/10.png"
}
]
}
}
}
I defined the following mapping:
我定义了以下映射:
RKObjectMapping *oiMapping = [RKObjectMapping mappingForClass:[OrderItem class]];
[oiMapping mapKeyPathsToAttributes:@"p_product_id", @"productID", @"name", @"name", @"t_event_id", @"eventID", @"quantity", @"quantity", nil];
[objectManager.mappingProvider setMapping:oiMapping forKeyPath:@"result.data.orderitems"];
RKObjectMapping *takeOrderResultMapping = [RKObjectMapping mappingForClass:[TakeOrderResult class]];
[takeOrderResultMapping mapKeyPathsToAttributes:@"errorCode", @"errorCode", @"errorMsg", @"errorMessage", nil];
[takeOrderResultMapping mapRelationship:@"orderitems" withMapping:oiMapping];
[objectManager.mappingProvider setMapping:takeOrderResultMapping forKeyPath:@"result"];
takeOrderResultMapping.rootKeyPath = @"result";
[[RKObjectManager sharedManager].mappingProvider setErrorMapping:takeOrderResultMapping];
And finally, the TakeOrderResult class is defined like this:
最后,TakeOrderResult类定义如下:
@interface TakeOrderResult : NSObject
@property (strong, nonatomic) NSNumber *errorCode;
@property (strong, nonatomic) NSString *errorMessage;
@property (strong, nonatomic) NSSet *orderitems;
@end
This more or less works - however, what Restkit returns is the an array of 3 Objects. The first object is an instance of TakeOrderResult with an empty set "orderitems". The next two objects are instances of OrderItems.
这或多或少有效 - 但是,Restkit返回的是3个对象的数组。第一个对象是TakeOrderResult的一个实例,其中有一个空集“orderitems”。接下来的两个对象是OrderItems的实例。
It would be great if somebody could help me and point out why I don't get 1 object, i.e. an instance of TakeOrderResult where "orderitems" is a set of two objects / two OrderItems.
如果有人可以帮助我并指出为什么我没有得到1个对象,即TakeOrderResult的实例,其中“orderitems”是一组两个对象/两个OrderItems,那将是很好的。
1 个解决方案
#1
1
Your mapping is slightly off. The reason you're getting 3 objects is you're adding both mappings to the mapping provider. The reason the "orderitems" is an empty set is because it doesn't know how to find the orderitems.
您的映射略有偏差。您获得3个对象的原因是您要将两个映射添加到映射提供程序。 “orderitems”是空集的原因是因为它不知道如何找到订单项。
I have no idea if it works, but i'd try the following. Don't add your oiMapping to the mapping provider. And instead of:
我不知道它是否有效,但我会尝试以下方法。不要将oiMapping添加到映射提供程序。而不是:
[takeOrderResultMapping mapRelationship:@"orderitems" withMapping:oiMapping];
[takeOrderResultMapping mapRelationship:@“orderitems”withMapping:oiMapping];
try:
尝试:
[takeOrderResultMapping addRelationshipMappingWithSourceKeyPath:@"data.orderitems" mapping:oiMapping];
[takeOrderResultMapping addRelationshipMappingWithSourceKeyPath:@“data.orderitems”mapping:oiMapping];
#1
1
Your mapping is slightly off. The reason you're getting 3 objects is you're adding both mappings to the mapping provider. The reason the "orderitems" is an empty set is because it doesn't know how to find the orderitems.
您的映射略有偏差。您获得3个对象的原因是您要将两个映射添加到映射提供程序。 “orderitems”是空集的原因是因为它不知道如何找到订单项。
I have no idea if it works, but i'd try the following. Don't add your oiMapping to the mapping provider. And instead of:
我不知道它是否有效,但我会尝试以下方法。不要将oiMapping添加到映射提供程序。而不是:
[takeOrderResultMapping mapRelationship:@"orderitems" withMapping:oiMapping];
[takeOrderResultMapping mapRelationship:@“orderitems”withMapping:oiMapping];
try:
尝试:
[takeOrderResultMapping addRelationshipMappingWithSourceKeyPath:@"data.orderitems" mapping:oiMapping];
[takeOrderResultMapping addRelationshipMappingWithSourceKeyPath:@“data.orderitems”mapping:oiMapping];