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

1.字符串转字典

NSString * jsonString = @“123”;

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

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

2.字典转字符串

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

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

你可能感兴趣的:(iOS开发中字符串和字典的转换)