比如我的代码:
AFHTTPRequestOperationManager *AFmanager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary *postDic = [self.userModel apiUserInfoDic];
[postDic setValue:[NSString JSONStringWithNSDictionary:self.orderModel.cartCids] forKey:@"cids"];
NSLog(@"%@", postDic);
NSDictionary *argsDic = [Core apiArgsFormat:postDic];
[AFmanager POST:[NSString stringWithFormat:@"%@/order/cart/preview", API_ROOT_URL] parameters:argsDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
postDic的数据结构如下;
2015-09-14 19:38:48.324[21743:4090197] {
cids = "[\n \"33\",\n \"34\",\n \"36\",\n \"35\"\n]";
machineId = "FAA19978-47B5-4086-8EF5-CFAA92FD59B4";
platform = iOS;
uid = 2;
}
我的本意是想让cids发送一个数组结构给后台,但是这样没法送到,不知道如何写呢?
最好的方式,应该是接口优化,不应该多层结构,最好是单层结构。也就是直接传字典,字典里全部都是简单的基本类型数据。
如果不能优化,那么就将数组转化成JSON字符串,或者将数组中的数组按照一定的规则来来拼接,比如a|b|c这样代替传数组,后台接收到时,就按照规则来分割得到数组了。
你都说了要数组,那为什么这么写?JSONStringWithNSDictionary?
[postDic setValue:[NSString JSONStringWithNSDictionary:self.orderModel.cartCids] forKey:@"cids"];
题主,如果你要传数组,那就一定要把你的数组转化成为JSONString再传。
你不能直接传cids,而是要传这个arrStr:NSString *arrStr = [cidsJSONString],然后再试试,就可以了。