我在发布到我的服务器时遇到问题。每个值都被解析期望我的数组的属性名称不是。
我的服务器期待这样的事情:
{“位置”: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)