0

我在发布到我的服务器时遇到问题。每个值都被解析期望我的数组的属性名称不是。

我的服务器期待这样的事情:

{“位置”:2,“_id”:“517808546b496658c10209”,“产品”:[{“金额”:3,“总计”:6.6,“名称”:“可乐”,“价格”:2.2},{“数量”:1,“总计”:4.0,“名称”:“水”,“价格”:2 } ] }

但我的客户正在发送这个:

{“位置”:2,“_id”:“517808546b496658c10209”,“产品”:[3、6.6、“可乐”、2.2、1、4.0、“水”、2]}

产品:

@property (nonatomic) NSNumber *_id;
@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *detail; 
@property (nonatomic, assign) float price; 
@property (nonatomic, strong) Category *category; 
@property (nonatomic, assign) int amount; 
@property (nonatomic, assign) float total;

我的代码:

RKObjectMapping *productMapping = [RKObjectMapping requestMapping];
[productMapping addAttributeMappingsFromDictionary:@{@"productId":@"_id",@"amount":@":amount",@"total":@"total",@"price":@"price",@"name":@"name"}];

RKObjectMapping *horecaMapping = [RKObjectMapping requestMapping];
[horecaMapping addAttributeMappingsFromDictionary:@{ @"Id": @"_Id",@"deliverySpotId":@"location"}];

[horecaMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"self.getBasketSet" toKeyPath:@"products" withMapping:productMapping]];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:horecaMapping objectClass:[Horeca class] rootKeyPath:nil];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseURL];
[manager addRequestDescriptor:requestDescriptor];
[manager postObject:sharedHoreca path:@"/orders" parameters:nil success:nil failure:nil];

--update 1-- 我也尝试为我的数组创建一个 NSSET,但这仍然对我没有帮助。 (nsarray 到 nsset restkit)

4

1 回答 1

0

self.getBasketSet关键路径让我停下来——返回的是什么?如果您的sharedHoreca对象返回一个NSArray响应指定的关键路径的对象,那么它应该做您所期望的......

于 2013-07-01T17:27:22.240 回答