JSON解析(序列化和反序列化)
2024-08-29 22:33:30
JSON的序列化,代码示例:
NSDictionary *dic = @{@"name":@"zhuzhu", @"age":@};
if (![NSJSONSerialization isValidJSONObject:dic]) {
NSLog(@"该对象不支持转换");
return;
}
/**
- Top level object is an NSArray or NSDictionary
- All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull
- All dictionary keys are NSStrings
- NSNumbers are not NaN or infinity
*/
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:kNilOptions error:nil];
NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonStr);
JSON的反序列化,代码示例:
NSDictionary *dic = @{@"name":@"zhuzhu", @"age":@};
if (![NSJSONSerialization isValidJSONObject:dic]) {
NSLog(@"该对象不支持转换");
return;
}
/**
- Top level object is an NSArray or NSDictionary
- All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull
- All dictionary keys are NSStrings
- NSNumbers are not NaN or infinity
*/
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:kNilOptions error:nil];
// 反序列的代码
NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]; NSLog(@"dic=%@", jsonDic);
最新文章
- 远程ssh登陆时报错:/bin/bash: Permission denied
- kafka_2.11-0.8.2.2的搭建
- mysql事件调度器定时删除binlog
- OpenCV -- 获取轮廓照片
- asp.net mvc jqgrid 同一个页面查询不同的表,jqgrid显示不同表的表头和数据并且分页
- c#转码解码
- 11g的alert日志路径
- 完整具体解释GCD系列(二)dispatch_after;dispatch_apply;dispatch_once
- # Instrument Time Profiler教程之Time Profiler
- CentOS6.x服务器OpenSSH平滑7.3p版本——拒绝服务器漏洞攻击
- hql语句中的select字句和from 字句
- [Python] timeit测试代码运行效率
- Docker Compose 配置文件常用指令
- P4774 [NOI2018]屠龙勇士
- kali虚拟机安装后操作[配置ssh,安装vmtools,更新源]
- 配置tomcat报错: Unknown version of Tomcat was specified.
- Ubuntu 14.04 下安装 TFTP 艰辛之路【转】
- SCP远程拷贝命令
- Hadoop源码解析 1 --- Hadoop工程包架构解析
- 题解【luogu4145 上帝造题的七分钟2(花神游历各国)】
热门文章
- 对象流,它们是一对高级流,负责即将java对象与字节之间在读写的过程中进行转换。 * java.io.ObjectOutputStream * java.io.ObjectInputStream
- 【实习第二天】odoo开发基础(二)
- 模拟超市付款 (if 多分支结构)
- 更改 undo_retention 时,Lob retention 不更改 (Doc ID 563470.1)
- Linux—修改文件权限、文件拥有者以及文件所在组
- Internet Download Manager是什么?
- 二、ITK例子-jpg图像读写
- Day12- Python基础12 线程、GIL、Lock锁、RLock锁、Semaphore锁、同步条件event
- 验证登录的前世今生:session、cookie
- 挑战编程 uva100 3n+1