iOS开发JSON字符串和字典互转
1、相关属性简述
NSJSONReadingOptions读取属性:
typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) { NSJSONReadingMutableContainers = (1UL << 0),// 返回可变容器 NSJSONReadingMutableLeaves = (1UL << 1), // 不仅返回的最外层是可变的, 内部的子数值或字典也是可变对象 NSJSONReadingAllowFragments = (1UL << 2)// 返回允许JSON字符串最外层既不是NSArray也不是NSDictionary,但必须是有效的JSON格式 } API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
NSJSONWritingOptions写入属性:
typedef NS_OPTIONS(NSUInteger, NSJSONWritingOptions) { NSJSONWritingPrettyPrinted = (1UL << 0),//是将生成的json数据格式化输出,这样可读性高,不设置则输出的json字符串就是一整行。 NSJSONWritingSortedKeys API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0)) = (1UL << 1)//输出的json字符串就是一整行 ios11.0之后 } API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
2、字典转JSON字符串
不论转JSON字符串,还是转回字典,都需要通过NSData这个桥梁!
2.1、如果NSJSONWritingOptions枚举为NSJSONWritingPrettyPrinted:
NSJSONWritingPrettyPrinted = (1UL << 0),//是将生成的json数据格式化输出,这样可读性高,不设置则输出的json字符串就是一整行。
- (NSString *)jsonStringOriWithDict:(NSDictionary *)dict{ NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString; if (!jsonData) { NSLog(@"%@",error); }else{ jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; } return jsonString; }
打印结果:很美观
但是此时的JSON字符串有空格和 \n ,是这个样子:
2.2、在之前基础上进行去空格和区 \n 操作,就能到达我们想要的JSON字符串纯种:
- (NSString *)jsonStringWithDict:(NSDictionary *)dict {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString;
if (!jsonData) {
NSLog(@"%@",error);
}else{
jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
NSRange range = {0,jsonString.length};
//去掉字符串中的空格
[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
NSRange range2 = {0,mutStr.length};
//去掉字符串中的换行符
[mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
return mutStr;
}
打印结果:就是一行
JSON字符串:
2.3、 在ios11.0之后推出新的枚举值:NSJSONWritingSortedKeys一次就可以解决空格和\n问题:
- (NSString *)jsonStringWithDict2:(NSDictionary *)dict{
NSError *error;
NSString *jsonString;
if (@available(iOS 11.0, *)) {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingSortedKeys error:&error];
if (!jsonData) {
NSLog(@"%@",error);
}else{
jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}
} else {
jsonString = [self jsonStringWithDict:dict];
}
return jsonString;
}
打印结果:也是一行
JSON字符串:
3、JSON字符串转字典
没什么可说的的,老代码一份:
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return nil; } NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; if(err) { NSLog(@"json解析失败:%@",err); return nil; } return dic; }
ForeverGuard博客园