iOS 数组/字典转json

xiaoxiao2021-02-28  8

NSMutableArray *tempArr = [NSMutableArray array]; //这里不要用for-in 先找到模型,然后给可变字典赋值,最后装到数组里 NSMutableDictionary *dic = [NSMutableDictionary dictionary]; for (int i = 0; i < self.dataSourceArr.count; i++) { PerponModel *model = self.dataSourceArr[i]; [dic setValue:model.name forKey:@"realname"]; [dic setValue:model.phoneNumber forKey:@"mobile"]; [tempArr addObject:dic]; } //dataWithJSONObject:后面这个是一个id类型的这样放数组就转数组,字典就转字典 //NSJSONWritingPrettyPrinted是一个枚举 NSData *data = [NSJSONSerialization dataWithJSONObject:tempArr options:NSJSONWritingPrettyPrinted error:nil]; NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
转载请注明原文地址: https://www.6miu.com/read-1100145.html

最新回复(0)