iOS中将数组作为参数传递给服务器

/ 0评 / 0

好像并没有发现iOS中直接放数组传的,所以我就把数组转化为相应的字符串来传递

方法一:

   NSMutableArray *array = [NSMutableArray array];
    for (Model *model in ——dataArray) {
        [array addObject:model.staffid];
    }
   //用逗号隔开
   [_dic setObject:[array componentsJoinedByString:@","] forKey:@"list"];

方法二:

NSMutableArray *array = [NSMutableArray array];
for (Model *model in ——dataArray) {
        [array addObject:model.staffid];
    }

//转换成data
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
[_dic setObject:[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding] forKey:@"list"];

评论已关闭。