iOS 开发中字典和字符串的转换

时间:2022-04-20 14:33:55

1.字符串转字典

NSString * jsonString = @"";

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];

2.字典转字符串

SData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];

NSString * str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];