我有一个POST API,我想用我的应用程序。它的必需参数如下所示:
parameter/type/field option/sample
id / int / required / 33
products / json/ required/ [{"id":"PREPAY_22223","quantity":1}]
card / string / option / 123456
使用JSONModel,我试图用这种方法发布我的数据:
- (RACSignal *)discount:(NSString *)planID withProducts:(NSArray *)product withCardf6:(NSString*)cardf6{
NSMutableDictionary *parameters = [self defaultParameters];
parameters[@"id"] = @(planID.intValue);
ProductJSON *p = [[EZProductJSON alloc]initWithDictionary:product[0] error:nil];
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:p, nil];
parameters[@"products"] = array;
parameters[@"card"] = cardf6;
parameters[@"local"] = @"en_US";
return [[[self rac_POST:@"plan/discount" parameters:parameters] map:^id(RACTuple *t) {
return t;
}] catch:^RACSignal *(NSError *error) {
return [self customErrorSignal:error];
}];
}
我一直收到一条错误消息,告诉我我正在使用无效的产品数据。我不知道为什么。我应该只是发送NSDictionary而不是JSON数据吗?
答案 0 :(得分:0)
您可以使用以下命令将数组转换为JSON字符串:
NSArray *myArray;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArray options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
并将jsonString
传递给API参数...
像这样:
- (RACSignal *)discount:(NSString *)planID withProducts:(NSArray *)product withCardf6:(NSString*)cardf6{
NSMutableDictionary *parameters = [self defaultParameters];
parameters[@"id"] = @(planID.intValue);
ProductJSON *p = [[EZProductJSON alloc]initWithDictionary:product[0] error:nil];
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:p, nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
parameters[@"products"] = array;
parameters[@"card"] = cardf6;
parameters[@"local"] = @"en_US";
return [[[self rac_POST:@"plan/discount" parameters:parameters] map:^id(RACTuple *t) {
return t;
}] catch:^RACSignal *(NSError *error) {
return [self customErrorSignal:error];
}];
}
希望这会帮助你.....