iOS開發中字串和字典的轉換

GabrielPanda發表於2018-01-24

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];
複製程式碼

相關文章