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];
iOS 数组/字典转json
最新推荐文章于 2024-07-07 03:25:55 发布
本文介绍如何使用Objective-C进行数组与字典的操作,包括创建可变数组和字典,并将模型对象的属性填充到字典中,再将字典添加到数组,最后将数组转换为JSON字符串。
341

被折叠的 条评论
为什么被折叠?



