【IOS开发基础系列】数据编解码专题

2022-03-08 14:36:27 浏览数 (1)

1 Base64

1.1 在iOS7中系统提供了base64的转码方式

示例如下:

NSData* originData = [originStr dataUsingEncoding: NSASCIIStringEncoding];

NSString* encodeResult = [originData base64EncodedStringWithOptions: NSDataBase64EncodingEndLineWithLineFeed];

NSLog(@"encodeResult:%@",encodeResult);

NSData* decodeData = [[NSData alloc] initWithBase64EncodedString: encodeResult options:0];

NSString* decodeStr = [[NSString alloc] initWithData: decodeData encoding: NSASCIIStringEncoding];

1.2 iOS使用开源库GTMBase64转码

/**

*GTM 转码

*/

- (void)GTMEncodeTest

{

NSString* originStr = @"Man";

NSString* encodeResult = nil;

NSData* originData = [originStr dataUsingEncoding: NSUTF8StringEncoding];

NSData* encodeData = [GTMBase64 encodeData: originData];

encodeResult = [[NSString alloc] initWithData: encodeData encoding: NSUTF8StringEncoding];

NSLog(@"%@",encodeResult);

}

/**

* GTM 解码

*/

- (void)GMTDecodeTest

{

NSString* encodeStr = @"TWFu";

NSString* decodeResult = nil;

NSData* encodeData = [encodeStr dataUsingEncoding: NSUTF8StringEncoding];

NSData* decodeData = [GTMBase64 decodeData: encodeData];

decodeResult = [[NSString alloc] initWithData: decodeData encoding: NSUTF8StringEncoding];

NSLog(@"%@",decodeResult);

}

2 JSON

2.1 JSON与NSDictionary互相转换

2.1.1 NSDictionary转换为NSString

- (NSString*) DataTOjsonString: (id)object

{

NSString*jsonString = nil;

NSError *error;

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

// Pass 0 if you don't care about the readability of the generated string

if (! jsonData) {

NSLog(@"Got an error: %@", error);

}else{

jsonString= [[NSString alloc] initWithData: jsonData encoding: NSUTF8StringEncoding];

}

return jsonString;

}

注意:

此时转换成的字符串中,每个键值对之间会预留一个回车换行符,导致http传输时会出错,因此如果要用于http网络传输时,需要手动将回车符替换掉!!!

jsonString = [jsonString stringByReplacingOccurrencesOfString: @"n" withString:@""];

2.1.2 将JSON串转化为NSDictionary或NSArray

// 将JSON串转化为字典或者数组

- (id)toArrayOrNSDictionary: (NSData*)jsonData{

NSError *error = nil;

id jsonObject = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingAllowFragments error: &error];

if (jsonObject != nil && error == nil){

return jsonObject;

}else{

// 解析错误

return nil;

}

}

3 参考链接

iOS Base64转码(使用ios7系统自带编码库 和GMTBase64两种方式)

http://www.2cto.com/kf/201405/297303.html

[译]iOS7:使用Base64编解码NSData和NSString对象

http://itony.me/488.html

iOS:Terminating app due to uncaught exception'NSInvalidArgumentException', reason: 'Invalid type in JSON write(_NSInlineData)'

http://stackoverflow.com/questions/24984098/iosterminating-app-due-to-uncaught-exception-nsinvalidargumentexception-reas

NSDictionary to jsonString ||对象转json格式

http://www.cnblogs.com/qingjoin/p/3437036.html

将NSString转换成UTF8编码的NSString

http://blog.csdn.net/a6472953/article/details/8163016

NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化

http://blog.csdn.net/xyxjn/article/details/17224733

0 人点赞