使用第三方Base64解码(data中包含中文)
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *jsonstring = [result base64DecodedString:gbkEncoding];
//解析json数据
NSData *jsondata = [jsonstring dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingMutableContainers error:nil];
NSString * tranErrorCode = [dic objectForKey:@"tranErrorCode"];
iOS7以后使用原生base64解码(data中包含中文)
NSStringEncoding gbkEncoding1 =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *result1 = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:result1 options:0];
NSString *jsonstring = [[NSString alloc] initWithData:decodedData encoding:gbkEncoding1];
//解析json
NSData *jsondata = [jsonstring dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingMutableContainers error:nil];
iOS7以后使用原生base64解码(data中不包含中文示例)
// 字符串转Data
NSString *str = @"encode string";
// 字符串转成Data
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];// 13 bytes
// 编码Data
NSData *encodeData = [data base64EncodedDataWithOptions:0];// 20 bytes
// 解码Data
NSData *decodeData = [[NSData alloc] initWithBase64EncodedData:encodeData options:0];// 13 bytes
// Data转字符串
NSString *decodeStr = [[NSString alloc] initWithData:decodeData encoding:NSUTF8StringEncoding];
NSLog(@"%@",decodeStr);// encode string