iOS基础 - 网络:JSON | JSONKit
▶ JSON
Javascript Object Notation:轻量级的数据交换格式,采⽤完全独立于语言的文本格式,被称为最理想的数据交换语言。其数据结构是:对象以 { 开始,以 } 结束;数组以 [ 开始,以 ] 结束
JSON 解析工具有:JSONKit、NSJSONSerialization、TouchJSON、SBJSON 等。其中 NSJSONSerialization 是系统提供的解析类,其解析效率最高
▶ NSJSONSerialization
iOS 5 中增加了 NSJSONSerialization,它在效率上完胜 SBJSON、TouchJSON、YAJL、JSONKit、NextiveJson ....使用 NSJSONSerialization 进行解析的前提
A. 顶层对象必须是 NSArray 或 NSDictionary
B. 所有对象必须是 NSString、NSNumber、NSArray、NSDictionary、NULL
C. 所有字典的键值都是字符串类型的
D. 数值不能是非数值或无穷大
代码示例:如何使用 NSJSONSerialization
// - Student.txt
[{"number":1,"name":"张三","sex":"男","phone":"18812345678"}, {"number":2,"name":"李四","sex":"女","phone":"18600009999"}, {"number":3,"name":"王五","sex":"男","phone":"13677889900"}]
// - 开始解析
1 // 路径 2 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"student" ofType:@"txt"]; 3 NSData *jsonData = [NSData dataWithContentsOfFile:filePath]; 4 5 NSError *error = nil; 6 // 把 JSON 字符串解析成 OC 的数据类型。options 参数是枚举值,在这里决定数组是可变的 7 NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 8 NSString *name = [[array objectAtIndex:0] objectForKey:@"name"]; 9 NSLog(@"%@",name); 10 NSLog(@"%@",array); 11 12 // 数据类型的相互转换 13 NSDictionary * userInfoDic = [NSDictionary dictionaryWithObjectsAndKeys:@"Hazell",@"userName",@"12345",@"password",@YES,@"isNewUser", nil]; 14 // 把 OC 的数据类型转化成 Data 15 NSData *jsonData01 = [NSJSONSerialization dataWithJSONObject:userInfoDic options:0 error:nil]; 16 NSLog(@"%@",jsonData01); 17 18 // 把 Data 数据转化成字符串 19 NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 20 NSLog(@"%@",jsonString);
▶ JSONKIT
JSON 是一种轻量级的数据格式,一般用于数据交互。服务器返回客户端的数据,一般都是 JSON格式 或者是 XML格式(文件下载除外)。JSON 本质上是一个特殊格式的字符串,注意不是 NSString!JSON解析 是一个非常繁琐的工作
JSONKIT 默认支持 MRC模式,如需在 ARC中使用,则在 Build Phases ->Compile Sources 选择文件双击 JSONKit,并在对话框中添加 -fno-objc-arc
代码示例:具体使用方式如下
1 #import "ViewController.h" 2 //#import "JSONKit.h" // 引入头文件 3 @implementation ViewController 4 5 - (void)viewDidLoad { 6 [super viewDidLoad]; 7 8 // 方式一 9 NSString *filePath02 = [[NSBundle mainBundle]pathForResource:@"student" ofType:@"txt"]; 10 NSString *jsonString02 = [NSString stringWithContentsOfFile:filePath02 encoding:NSUTF8StringEncoding error:nil]; 11 12 // 把 JSON 字符串解析成 OC 数据类型 13 NSArray *array02 = [jsonString02 objectFromJSONString]; 14 NSLog(@"%@",array02); 15 16 // 方式二 17 NSString *filePath03 = [[NSBundle mainBundle] pathForResource:@"student" ofType:@"txt"]; 18 NSData *jsonData03 = [NSData dataWithContentsOfFile:filePath03]; 19 NSArray *array03 = [jsonData03 objectFromJSONData]; 20 NSLog(@"%@",array03); 21 22 NSDictionary *userInfoDic05 = [NSDictionary dictionaryWithObjectsAndKeys:@"Hazell",@"account",@12345,@"password", nil]; 23 // 把 OC 数据转化成 JSON 字符串 24 NSString *jsonString05 = [userInfoDic05 JSONString]; 25 NSLog(@"%@",jsonString05); 26 // 把 OC 数据类型转化成 JsonData 27 NSData *jsonData05 = [userInfoDic05 JSONData]; 28 NSLog(@"%@",jsonData05); 29 } 30 31 @end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律