JSON
iOS中的解析方案:
第三方:JSONKit, SBJson, TouchJSON
苹果原生 NSJSONSerialization(性能最好)
NSJSONSerialization
JSON -> OC对象
- (void)jsonToOc
{
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
/*
* 参数1:json二进制数据
* 参数2:
* NSJSONReadingMutableContainers = (1UL << 0), 可变字典和数组
* NSJSONReadingMutableLeaves = (1UL << 1), 内部所有字符串是可变的(iOS7以后有问题,一般用不到)
* NSJSONReadingAllowFragments = (1UL << 2) 既不是字典也不是数组,使用这个枚举
* 参数3:错误信息
*/
NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dict);
}];
}
JSON -> OC对象之间的数据转换关系
- (void)jsonWithOc
{
NSString *str = @"\{\"error\":\"用户名不存在\"}"; //options:NSJSONReadingMutableContainers //输出: NSDictionary----{error = "\U7528\U6237\U540d\U4e0d\U5b58\U5728";}
//NSString *str = @"[\"team1\",\"team2\"]"; //options:NSJSONReadingMutableContainers //输出: NSArray----(team1,team2)
//NSString *str = @"\"fewcfrvfecwscd\""; //options:NSJSONReadingAllowFragments //输出: NSMutableString----fewcfrvfecwscd
//NSString *str = @"false"; //options:NSJSONReadingAllowFragments //输出: NSNumber----1
//NSString *str = @"true"; //options:NSJSONReadingAllowFragments //输出: NSNumber----1
//NSString *str = @"null"; //options:NSJSONReadingAllowFragments //输出:superclass:NSObjcect class:NSNull value <null>
id obj = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@----%@",[obj superclass],obj);
}
总结以上特点:
JSON | OC |
---|---|
{} | @{} |
[] | @[] |
"" | @"" |
false | NSNumber 0 |
true | NSNumber 1 |
null | NSNull |
OC对象 -> JSON
- (void)ocToJson
{
NSDictionary *dictM = @{@"name":@"张帆",@"age":@32};
/*
* 参数1:要转换的OC对象
* 参数2:
* NSJSONWritingPrettyPrinted 排版美观
* 参数3:错误信息
*/
NSString *strM = @"vevrevverwcferw";
BOOL isValid = [NSJSONSerialization isValidJSONObject:strM];
/*
- 最外层的对象必须是 NSArray or NSDictionary
- 所有的元素必须是 NSString, NSNumber, NSArray, NSDictionary, or NSNull
- 字典的键必须是 NSStrings
- NSNumbers 不能为 NaN 或 infinity(无穷大)
*/
if(!isValid)
{
NSLog(@"该数据类型无法转换为json");
return;
}
NSData *data = [NSJSONSerialization dataWithJSONObject:dictM options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}
plist文件转json文件存储
- (void)plistFileToJsonFile
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil];
NSArray *dicts = [NSArray arrayWithContentsOfFile:path];
NSLog(@"%@",dicts);
NSData *data = [NSJSONSerialization dataWithJSONObject:dicts options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@"%@",data);
NSString *path2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *fileName = @"app.json";
NSString *fullPath = [path2 stringByAppendingPathComponent:fileName];
NSLog(@"%@",fullPath);
[data writeToFile:fullPath atomically:YES];
}