iOS 数组/字典转json
NSMutableArray *tempArr = [NSMutableArray array];//这里不要用for-in 先找到模型,然后给可变字典赋值,最后装到数组里NSMutableDictionary *dic = [NSMutableDictionary dictionary];for (int i = 0; i < self.dataSourceArr.coun
·
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];
更多推荐
所有评论(0)