0

AFNetworking 不想在 JSON 请求中将 BOOL 值表示为真/假。尝试了@YES,NSNumber 但没有机会,它转换为 0 或 1。

NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setValue:@YES forKey:@"favorite"];

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:data forKey:@"data"];

然后将此参数字典提供给 AfNetworking 3

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

manager.requestSerializer = serializer;


[manager POST:@"HTTPS://JSON URL" parameters:params progress:^(NSProgress *uploadProgress) {
} success:^(NSURLSessionDataTask * task, id responseObject) {
//SUCCESS
} failure:^(NSURLSessionDataTask *task, NSError *error) {
//FAILED REQUEST
}];
4

2 回答 2

0

这就是我切换布尔值的方式,然后使用 AFNetworking 将它们作为 JSON 布尔值发送:

    self.petActive = ! self.petActive;
    NSNumber* newFMPstate = self.petActive ? @YES : @NO;
    NSDictionary *fmpDict = @{ @"petActive": newFMPstate };
    /// ... assemble endpoint for put and pass it fmpDict

@YES 和 @NO 分别生成这个 JSON

{"petActive":true}  
{"petActive":false} 

(我知道三元运算符是多余的,但似乎增加了可读性)

于 2017-12-06T17:00:19.660 回答
0

利用[NSNumber numberWithBool:YES]

于 2016-04-27T20:07:58.740 回答