我需要将数组转换为json并将其作为参数发送到url以及其他参数。我使用jsonKit进行json转换,使用AFNetworking进行网络通信。但是它总是在服务器的json中给出异常。
以下是用于儿子转换的代码:
NSString *jsonData = [self.finalArray JSONString];
NSString *data = [NSString stringWithFormat:@"%s",[jsonData UTF8String] ];
此数据最终作为url中的参数发送。 网络通信代码:
postData=[postData stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *requestUrl = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",url,postData]];
NSURLRequest *request =[[NSURLRequest alloc] initWithURL:requestUrl];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
[operation setCompletionBlockWithSuccess:success failure:failure];
[operation start];
也用于转义字符:
[postData stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
收到错误:
错误域= NSURLErrorDomain代码= -1002"不支持的网址" UserInfo = {NSUnderlyingError = 0x7d51e9a0 {错误域= kCFErrorDomainCFNetwork代码= -1002"不支持的URL" UserInfo = {NSLocalizedDescription =不支持的网址}},NSLocalizedDescription =不支持的网址}