AFNetworking 2.0 将 [] 方括号添加到发送到服务器的 json
Posted
技术标签:
【中文标题】AFNetworking 2.0 将 [] 方括号添加到发送到服务器的 json【英文标题】:AFNetworking 2.0 adds [] square brackets to json that get send to server 【发布时间】:2014-02-24 14:32:59 【问题描述】:我正在使用 AFNetworking 2.0,但遇到了问题。
我正在尝试将 json 中的数组放入服务器。
我正在发布类似的数组(请参阅下面的代码)。我将以下数组添加到 Json 参数以发送到服务器:
themes = (
"Fashion - Men",
Kids,
"Styling / Hair"
);
这会被发送到服务器:
id = 654;
tags = test;
themes = (
"Fashion - Men",
Kids,
"Styling / Hair"
);
但是服务器收到这样的 json 是错误的:
'id': '654', 'themes[]': ['Kids', 'Styling / Hair', 'Accessories - Women'], 'tags': 'test'
AFNetworking 在发送的 json 中按主题放置 [] 方括号,例如主题 [],因此 cal 是正确的,除了这个:主题 []
不知道如何解决这个问题。
- (void) piccMedia: (NSString*) aPiccId Tags: (NSString *) aTags Themes: (NSMutableArray *) anThemes
success:(void (^)(NSArray *response))success
failure:(void (^)(NSError *error))failure
NSMutableDictionary * params = [[NSMutableDictionary alloc] init];
[params setObject: @"654" forKey:@"id"];
[params setObject: aTags forKey:@"tags"];
[params setObject: anThemes forKey:@"themes"];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *piccingAuthToken = [prefs stringForKey:@"piccingAuthToken"];
NSString *piccingUsername = [prefs stringForKey:@"piccingUsername"];
NSLog(@"piccingAuthToken %@ %@", piccingAuthToken,piccingUsername);
NSString *postQueryString = [NSString stringWithFormat:@"http://dev.com/services/rest/profiles/%@/media",piccingUsername];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [JSONResponseSerializerWithData serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", @"text/html", nil];
manager.responseSerializer = [JSONResponseSerializerWithData serializerWithReadingOptions:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves];
[manager.requestSerializer setValue:piccingAuthToken forHTTPHeaderField:@"x-authtoken"];
[manager PUT:postQueryString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
success(responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
id json = error.userInfo [JSONResponseSerializerWithDataKey];
failure(json);
];
2014-02-24 16:49:25.265 App[7314:70b] JSON Parameters
id = 654;
tags = test;
themes = (
"Accessories - Women",
"Styling / Hair",
Kids
);
【问题讨论】:
NSLog
params 并将输出添加到您的答案中。
params can more easily be created:
NSDictionary *params = @ @"id":@"654", @"tags":aTags, @"themes":anThemes;`
什么是JSONResponseSerializerWithData
? AFNetworking 没有。可以发一下代码吗?
JSONResponseSerializerWithData 只是帮助我更好地解释我的错误
抱歉。 JSONResponseSerializerWithData 确实带有 AFNetworking
【参考方案1】:
您应该将 requestSerializer 设置为 AFJSONRequestSerializer
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"JSON: %@", responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
【讨论】:
以上是关于AFNetworking 2.0 将 [] 方括号添加到发送到服务器的 json的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking 2.0 将 NSURLSessionDataTask 转换为 NSURLSessionDownloadTask 不会将所有文件数据写入磁盘