iOS - OC - JSON 解析 - NSJSONSerialization

  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 }

猜你喜欢

转载自www.cnblogs.com/qingzZ/p/9293733.html
今日推荐