我正在尝试通过 POST 将参数发送到我的服务器,它通常可以工作,但我无法弄清楚如何发送包含数组作为参数之一的 JSON。这是我尝试过的:
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:myURL]];
NSMutableArray *objectsInCart = [NSMutableArray arrayWithCapacity:[_cart count]];
for(NSDictionary *dict in _cart)
{
NSObject *object = [dict objectForKey:@"object"];
NSDictionary *objectDict = @{@"product_id": [NSString stringWithFormat:@"%d",[object productID]],
@"quantity": [NSString stringWithFormat:@"%d", [[dict objectForKey:@"count"] intValue]],
@"store_id": [NSString stringWithFormat:@"%d", [Store getStoreID]],
@"price": [NSString stringWithFormat:@"%.2f", [object price]]};
[objectsInCart addObject:objectDict];
}
NSError *error = nil;
NSString *cartJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart
options:NSJSONWritingPrettyPrinted
error:&error]
encoding:NSUTF8StringEncoding];
if(error)
{
NSLog(@"Error serializing cart to JSON: %@", [error description]);
return;
}
NSDictionary *parameters = @{@"status": @"SUBMITTED",
@"orders": cartJSON};
NSMutableURLRequest *orderRequest = [httpClient requestWithMethod:@"POST"
path:@"/app/carts"
parameters:parameters];
AFJSONRequestOperation *JSONOperation = [[AFJSONRequestOperation alloc] initWithRequest:orderRequest];
但是,发送此 JSON 时出现错误。任何建议都非常感谢!