iOS-----JSON解析

JSON解析

JSON是一种广泛使用的数据交换格式,JSON同样具有跨平台、跨语言的优势,而且使用JSON作为数据交换格式时数据传输量更小。

JSON的基本知识

JSON的全称是JavaScript Object Notation,即JavaScript 对象符号,它是一种轻量级的数据交换格式.JSON数据格式既适合人进行读/写,也适合计算机本身解析和生成。

JSON主要有如下两种数据结构

由key-value对组成的数据结构,在不同的语言中有不同的实现。例如,在JavaScript中是一个对象,在Objective-C中是一种NSDictionary对象,在C语言中则是一个struct,在其他语言中可能有record、dictionary、hash table等实现

有序集合,在不同语言中可能有NSArray、vector、数组和序列等实现。

在JavaScript中主要有两种JSON语法,其中一种用于创建对象,另一种用于创建数组。

使用JSON语法创建对象

   JSON创建对象时,总是{开始,以}结束,对象的每个属性名和属性值之间以英文冒号(:)隔开,多个属性定义之间以英文逗号(,)隔开.语法格式如下:

 1 object  =
 2 
 3 {
 4 
 5      propertyName1 : propertyValue1,
 6 
 7       propertyName2 : propertyValue2,
 8 
 9 10 
11 }

使用JSON语法创建数组

  使用JSON语法创建数组总是以英文方括号( [ )开始,然后依次放入数组元素,元素与元素之间以英文逗号( , )隔开,最后一个数组元素后面不需要英文逗号,但以英文反方括号 ( ] ).使用JSON创建数组的语法格式如下:

arr = [value1 , value2,…];

对于Objective-C的JSON工具而言,同样提供类似的两个功能

将Objective-C对象转换成JSON格式的字符串

将JSON格式的字符串恢复成Objective-C对象

使用NSJSONSerialization处理JSON数据

NSJSONSerialization类提供了如下类方法来支持JSON解析或生成

 

NSJSONSerialization只能将满足如下条件的对象转换为JSON数据

 

使用SBJson解析JSON数据

     SBJson同样可以完成双向转换,它最主要的连个工具类就是SBJsonParser和SBJsonWriter,

其中SBJsonParser负责把NSData或NSString形式的JSON数据转换为Objective-C对象;

而SBJsonWriter则负责把Objective-C对象转换为NSData或NSString形式的JSON数据.

使用JSONKit解析JSON数据

  1 ViewController.m
  2 
  3 #import “ViewController.h”
  4 
  5 #import “SBJson.h”
  6 
  7 #import “JSONKit.h”
  8 
  9 @implementation ViewController
 10 
 11 NSArray* books;
 12 
 13 NSArray* parseResult;
 14 
 15 NSString* tableTitle;
 16 
 17 - (void)viewDidLoad
 18 
 19 {
 20 
 21    [super viewDidLoad];
 22 
 23   // 定义一个Objective-C的NSArray数据,该示例会使用3种方式将该对象转换为JSON数据
 24 
 25  books = [NSArray arrayWithObjects:
 26 
 27 [NSDictionary  dictionaryWithObjectsAndKeys@”红楼梦”, @”title”,
 28 
 29 @”曹雪芹 编著” , @”author”,
 30 
 31 @”书写了清朝历史豪门的缩影”, @”remark” , nil ] ,
 32 
 33 [NSDictionary  dictionaryWithObjectsAndKeys@”三国演义”, @”title”,
 34 
 35 @”罗贯中 编著” , @”author”,
 36 
 37 @”天下分久必合合久必分”, @”remark” , nil ] ,
 38 
 39 [NSDictionary  dictionaryWithObjectsAndKeys@”西游记”, @”title”,
 40 
 41 @”吴承恩 编著” , @”author”,
 42 
 43 @”去西天取经”, @”remark” , nil ] ,
 44 
 45 [NSDictionary  dictionaryWithObjectsAndKeys@”水浒传”, @”title”,
 46 
 47 @”施耐庵 编著” , @”author”,
 48 
 49 @”草根”, @”remark” , nil ]
 50 
 51 ,nil];
 52 
 53 self.tableView.dataSource = self;
 54 
 55 }
 56 
 57 - (IBAction)JSONSerializationParser:(id)sender
 58 
 59 {
 60 
 61    // 获取JSON文件所在的路径
 62 
 63   NSString* jsonPath = [[NSBundle mainBundle] pathForResource:@”books”
 64 
 65 ofType@”json”];
 66 
 67 // 读取jsonPath对应文件的数据
 68 
 69 NSData* data = [NSData dataWithContentsOfFile:jsonPath];
 70 
 71 // 解析JSON数据,返回Objective-C对象
 72 
 73 parseResult = [NSJSONSerialization JSONObjectWithData:data
 74 
 75 options:0 error:nil];
 76 
 77 tableTitle = @”使用JSONSerializationParse解析”;
 78 
 79 // 让tableView重新加载数据
 80 
 81 [self.tableView reloadData];
 82 
 83 }
 84 
 85 - (IBAction)SBParser:(id)sender
 86 
 87 {
 88 
 89    // 获取JSON文件所在的路径
 90 
 91   NSString* jsonPath = [[NSBundle mainBundle] pathForResource:@”books”
 92 
 93 ofType:@”json”];
 94 
 95 // 读取jsonPath对应文件的数据
 96 
 97 NSData* data = [NSData dataWithContentsOfFile:jsonPath];
 98 
 99 // 创建SBJsonParser对象
100 
101 SBJsonParser* jsonParser = [[SBJsonParser alloc] init];
102 
103 parserResult = [jsonParser objectWithData:data];
104 
105 tableTitle = @”使用SBJson解析”;
106 
107 // 让tableView重新加载数据
108 
109 [self.tableView reloadData];
110 
111 }
112 
113 - (IBAction)JSONKitParse:(id)sender
114 
115 {
116 
117    // 获取JSON文件所在的路径
118 
119   NSString* jsonPath = [[NSBundle mainBundle] pathForResource:@”books”
120 
121 ofType:@”json”];
122 
123 //  读取jsonPath对应文件的数据
124 
125 NSData* data = [NSData dataWithContentsOfFile:jsonPath];
126 
127 //  调用JSONKit为NSData扩展的objectFromJSONData方法解析JSON数据
128 
129 parseResult = [data objectFromJSONData];
130 
131 tableTitle = @”使用JSONKit解析”;
132 
133  //  让tableView重新加载数据
134 
135 [self.tableView reloadData];
136 
137 }
138 
139 - (IBAction)JSONSerializtionGenerate:(id)sender
140 
141 {
142 
143    // 调用NSJSONSerialization的类方法将Objective-C对象转换为JSON数据
144 
145    NSData* data = [NSJSONSerialization dataWithJSONObject:books
146 
147 options:0  error: nil];
148 
149 //  将NSData格式的JSON数据转换为字符串
150 
151 NSString* json = [[NSString alloc] initWithData:data
152 
153 encoding:NSUTF8StringEncoding];
154 
155 NSLog(@”NSJSONSerialization 转换得到的字符串为:%@”,  json);
156 
157 }
158 
159 - (IBAction)SBGenerate:(id)sender
160 
161 {
162 
163    // 创建SBJsonWriter对象
164 
165   SBJsonWriter* jsonWriter = [[SBJsonWriter alloc] init];
166 
167   // 调用SBJsonWriter对象的方法将Objective-C对象转换为JSON字符串
168 
169   NSLog(@”SBJson 转换得到字符串为:  %@”, [jsonWriter stringWithObject: books]);
170 
171 }
172 
173 - (IBAction)JSONKitGenerate:(id)sender
174 
175 {
176 
177   // 调用JSONKit为NSArray对象扩展的JSONString方法将Objective-C对象转换为JSON字符串
178 
179   NSLog(@”JSONKit 转换得到字符串为:  %@”, [books JSONString]);
180 
181 }
182 
183 - (NSString*)tableView:(UITableView *)tableView
184 
185 titleForHeaderInSection:(NSInteger)section
186 
187 {
188 
189   return  tableTitle;
190 
191 }
192 
193 - (NSInteger)tableView:(UITableView*)tableView
194 
195   numberOfRowsInSection: (NSInteger)section
196 
197 {
198 
199    // parseResult中包含多少个对象,该表格就显示多少行
200 
201    retrun [parseResult  count];
202 
203 }
204 
205 - (UITableViewCell*)tableView:(UITableView*)tableView
206 
207  cellForRowAtIndexPath:(NSIndexPath*)indexPath
208 
209 {
210 
211    // 根据动态单元格原型的ID来获取可重用单元格
212 
213   UITableViewCell* cell = [tableView  dequeueReusableCellWithIdentifier:@”bookCell”
214 
215 forIndexPath: indexPath];
216 
217 // 获取当前行号在parserResult中对应的数据
218 
219 NSDictionary* book = [parseResult  objectAtIndex: indexPath.row];
220 
221 // 获取单元格中的3个控件,并为3个控件设置显示文本
222 
223 UILabel* titleLabel = (UILabel*)[cell  viewWithTag:1];
224 
225 titleLabel.text = [book objectForKey:@”title”];
226 
227 UILabel* authorLabel = (UILabel* )[cell viewWithTag:2];
228 
229 authorLabel.text = [book objectForKey:@”author”];
230 
231 UILabel* remarkLabel = (UILabel* )[cell viewWithTag:2];
232 
233 authorLabel.text = [book objectForKey:@”remark”];
234 
235 }
236 
237 @end
原文地址:https://www.cnblogs.com/congli0220/p/5067953.html