1. 程式人生 > >iOS - OC - JSON 解析 - NSJSONSerialization

iOS - OC - JSON 解析 - NSJSONSerialization

init ffi {} leave content 枚舉值 ring oct sda

  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 }

iOS - OC - JSON 解析 - NSJSONSerialization