iOS 上传array参数

之前做项目的时候,后台要求传入数组:
WeChat8c6483af44fa5da95e18fffe24507738.png

众所周知,iOS上传的时候parameters类型都是字典NSDictionary,后台也不可能为了你一个人该接口参数接收方式,找了很久解决方法如下:

 [BaseNetWorking requestURL:url withParam:[arrr mj_JSONObject]   withMethodType:kXMHTTPMethodPOST withSuccessBlock:^(id  _Nullable responseObject) {
        if ([responseObject[@"code"] intValue]==200) {

            [SVProgressHUD showSuccessWithStatus:@"提交成功"];
           
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];

        }


    } withFailure:^(NSError * _Nullable error) {

    }];

如果没有用MJExtension
的话,原始代码如下:

  NSArray *arr = @[@"11",@"22",@"33",];
            
  NSError *parseError = nil;

  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted  error:&parseError];
            
 NSString *jsonstr =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            
 NSData *objectData = [jsonstr dataUsingEncoding:NSUTF8StringEncoding];
            
 NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&parseError];

你可能感兴趣的:(iOS 上传array参数)