iOS 中 JSON 数据交换格式
JSON (JavaScript Object Notation)是一种轻量级的数据交换格式。
JSON 的详细教程,能够參见 JSON 中国:http://www.json.org.cn/index.htm ,当然还有
JSON 在线校验格式化工具:http://www.bejson.com/ ,希望深入学习 JSON 能够參考其它教程。JSON 的轻量级是相对于 XML 文档结构而言的,描写叙述项目字符少。所以描写叙述同样的数据所需的字符个数要少,当然传输的速度就会提高而流量也会降低。
像微博,对于微博服务端数据非常多就是 JSON ,须要对这些请求回来的数据进行本地解析。由于 Web 和移动平台开发对流量要求要尽可能的少,对速度要求又要比較快,所以 JSON 就成为理想的数据交换语言。
把数据写成 JSON 结构过程称为“编码”过程,即是写入过程。把数据从 JSON 文档中读取处理的过程称为“解码”过程,即是解析和读取过程。
以下列举一些 JSON 经常使用的解析框架:
(1)SBJson
(2)TouchJSON
(3)YAJL 。基于SBJson,里面进行了优化,底层 API 使用 C 编写,上层 API 是 objective-c 编写。使用者能够有多不同的选择。
它不支持 ARC ,源代码下载地址:http://lloyd.github.com/yajl/
(4)JSONKit
(5)NextiveJson ,此框架不支持 ARC 。源代码下载地址:http://github.com/nextive/NextiveJson
(6)NSJSONSerialization 。从这个框架的命名规则来看。想你也猜到了它是苹果自家的 JSON 解析框架,它是 iOS5 之后提出来的。
详细的简单实例也是能够參考这篇博客:http://blog.csdn.net/enuola/article/details/7903632/ 有些方法在 iOS7 以后的版本号中会有警告,把它改动为 iOS7
以后支持的方法便可。
以下来实现一个利用 NSJSONSerialization 来进行解析的简单实现,当然也是能够用第三方框架,下载源代码。安装,假设不支持 ARC 配置便可。直接在 viewDidLoad 方法中进行解析
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem; self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController]; [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadView:)
name:@"reloadViewNotification"
object:nil]; NotesNextiveJsonParser *parser = [NotesNextiveJsonParser new];
//開始解析
[parser start]; NSString* path = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"json"]; //解析一个叫 Test.json 的文件
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path]; NSError *error; id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers error:&error]; if (!jsonObj || error) {
NSLog(@"JSON解码失败");
}
self.listData = [jsonObj objectForKey:@"Record"]; }
如有不正确,还望指点。
最新文章
- python方法中的self
- 学习设计模式第三 - 基础使用UML表示关系
- SQLite安装、编译与应用
- Unity 联网小测试(WWW)
- SQL Server 取日期时只要年月或年月日
- 论文笔记之:Localizing by Describing: Attribute-Guided Attention Localization for Fine-Grained Recognition
- (Problem 62)Cubic permutations(待续)
- Microsoft Office 2016 简体中文 Vol 版镜像下载(Pro Plus、Visio、Project 下载)
- 当react框架遇上百度地图
- Python爬虫番外篇之关于登录
- vb.net 数字大写
- 设计模式---数据结构模式之组合模式(Composite)
- sublime Text3基本配置记录+python
- ionic3 创建项目至apk打包全过程教程
- python logging日志输出个文件中
- android 系统层 常用类介绍
- 【Coursera】Security Introduction -Eighth Week(1)
- repeater嵌套RadioButtonList赋值
- pywin32记录备忘
- [Eclipse]Eclipse快捷键
热门文章
- java入门概念梳理总结
- JDK Windows安装
- npm 安装或更新模块失败的解决办法
- Check if a user is in a group
- Redux 洋葱模型理解
- 【推导】【DFS】Codeforces Round #429 (Div. 1) B. Leha and another game about graph
- 网络编程之tcp五层模型
- 1前端案例-tag标签+随机位置
- PVS-Studio静态通用分析规则
- iOS 在某一个ViewController跳转到TabViewController中的某一个ViewController