OC_字典与Json字符串相互转换

// 字典转换成Json字符串  
NSDictionary * testDict = @{@"hid":@"1",  
                            @"cut_price":@"2",  
                            @"cut_num":@"3",  
                            @"goods_id":@"4",  
                            @"pool_id":@"5"};  
NSError * error = nil;  
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:testDict options:NSJSONWritingPrettyPrinted error:&error];  
NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];  
  
// 将json字符串转换成字典  
NSData * getJsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];  
NSDictionary * getDict = [NSJSONSerialization JSONObjectWithData:getJsonData options:NSJSONReadingMutableContainers error:&error];  

你可能感兴趣的:(OC_字典与Json字符串相互转换)