1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 10 { 11 [self test]; 12 } 13 14 -(void)jsonToOC 15 { 16 //1.确定url 17 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=456&type=JSON"]; 18 19 //2.创建请求对象 20 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 21 22 //3.发送异步请求 23 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 24 //data---->本质上是一个json字符串 25 //4.解析数据 26 //NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]); 27 28 //JSON--->oc对象 反序列化 29 /* 30 第一个参数:JSON的二进制数据 31 第二个参数: 32 第三个参数:错误信息 33 */ 34 /* 35 NSJSONReadingMutableContainers = (1UL << 0), 可变字典和数组 36 NSJSONReadingMutableLeaves = (1UL << 1), 内部所有的字符串都是可变的 ios7之后又问题 一般不用 37 NSJSONReadingAllowFragments = (1UL << 2) 既不是字典也不是数组,则必须使用该枚举值 38 */ 39 40 NSString *strM = @"\"wendingding\""; 41 42 // NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 43 44 id obj = [NSJSONSerialization JSONObjectWithData:[strM dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil]; 45 46 NSLog(@"%@---%@",[obj class],obj); 47 48 }]; 49 50 } 51 52 //JSON--->OC 53 -(void)JSONWithOc 54 { 55 //NSString *strM = @"{\"error\":\"用户名不存在\"}"; 56 //NSString *strM = @"[\"error\",\"用户名不存在\"]"; 57 //NSString *strM = @"\"wendingding\""; 58 //NSString *strM = @"false"; 59 //NSString *strM = @"true"; 60 NSString *strM = @"null"; 61 62 id obj = [NSJSONSerialization JSONObjectWithData:[strM dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:0]; 63 NSLog(@"%@---%@",[obj class],obj); 64 65 /* 66 JOSN OC 67 {} @{} 68 [] @[] 69 "" @"" 70 false NSNumber 0 71 true NSNumber 1 72 null NSNull为空 73 */ 74 75 //nil 76 [NSNull null]; //该方法获得的是一个单粒,表示为空,可以用在字典或者是数组中 77 78 } 79 80 //OC--->json 81 -(void)OCtojson 82 { 83 NSDictionary *dictM = @{ 84 @"name":@"dasheng11", 85 @"age":@3 86 }; 87 88 NSArray *arrayM = @[@"123",@"456"]; 89 90 //注意:并不是所有的OC对象都能转换为JSON 91 /* 92 - 最外层必须是 NSArray or NSDictionary 93 - 所有的元素必须是 NSString, NSNumber, NSArray, NSDictionary, or NSNull 94 - 字典中所有的key都必须是 NSStrings类型的 95 - NSNumbers不能死无穷大 96 */ 97 NSString *strM = @"WENIDNGDING"; 98 99 BOOL isValid = [NSJSONSerialization isValidJSONObject:strM]; 100 if (!isValid) { 101 NSLog(@"%zd",isValid); 102 return; 103 } 104 105 //OC--->json 106 /* 107 第一个参数:要转换的OC对象 108 第二个参数:选项NSJSONWritingPrettyPrinted 排版 美观 109 */ 110 NSData *data = [NSJSONSerialization dataWithJSONObject:strM options:NSJSONWritingPrettyPrinted error:nil]; 111 112 NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]); 113 } 114 115 -(void)test 116 { 117 NSArray *arrayM = [NSArray arrayWithContentsOfFile:@"/Users/xiaomage/Desktop/课堂共享/11大神班上课资料/05-多线程网络/0225/资料/apps.plist"]; 118 NSLog(@"%@",arrayM); 119 120 //[arrayM writeToFile:@"/Users/xiaomage/Desktop/123.json" atomically:YES]; 121 122 //OC--->JSON 123 NSData *data = [NSJSONSerialization dataWithJSONObject:arrayM options:NSJSONWritingPrettyPrinted error:0]; 124 [data writeToFile:@"/Users/xiaomage/Desktop/123.json" atomically:YES]; 125 }
分类:
OC 网络请求
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现