向服务器传送json数据

NSString *url = @"http://47.92.82.159:8888/home/user/reg";

//1.创建一个操作请求的管理证

AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

//请求参数

mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/x-javascript",@"text/json", @"application/json", nil];

NSDictionary *dict = [NSDictionary dictionary];

dict = @{@"user_info":@{@"mobile":@"13112345678",@"user_name":@"11111",@"password":@"123456"}};

NSError *error;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

NSString *jsonString;

if (!jsonData) {

NSLog(@"%@",error);

}else{

jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

}

NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];

NSLog(@"111111 = %@",jsonString);

NSRange range = {0,jsonString.length};

//去掉字符串中的空格

[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];

NSRange range2 = {0,mutStr.length};

//去掉字符串中的换行符

[mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

[mgr POST:url parameters:@{@"reg_map":[NSString stringWithFormat:@"'%@'", jsonString]} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

NSLog( @"responseObject = %@",responseObject);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

}];

你可能感兴趣的:(向服务器传送json数据)