JSON之解析
JSON之解析通过TouchJSON\SBJSON\JSONKit\NSJSONSerialization
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写,同时也易于机器解析和生成。格式:{ "firstName": "a","secondName":"b" }
NSJSONSerialization是iOS5开始自带的JSON解析API,且效率较高,解析的速度快
TouchJSON\SBJSON\JSONKit是第三方库
下载链接:
TouchJSON:https://github.com/TouchCode/TouchJSON
SBJSON:https://github.com/stig/json-framework
JSONKit:https://github.com/johnezang/JSONKit
在解析的效率上NSJSONSerialization>JSONKit>TouchJSON>SBJSON
以下是解析http://m.weather.com.cn/data/101010100.html,获得天气的不同方法
JSON内容是{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2013年3月9日",,,,,,,,}}多层嵌套
一.NSJSONSerialization
解析方法:
NSDictionary *weatherDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
案列:
二.JSONKit
解析方法:
#import "JSONKit.h"
1.
JSONDecoder *decoder=[[JSONDecoder alloc]init];
NSDictionary *weatherDic=[decoder objectWithData:data];
2.
NSDictionary *weatherDic=[data objectFromJSONData];
案列:
三.TouchJSON
解析方法:
#import "CJSONDeserializer.h"
NSDictionary *weatherDic=[[CJSONDeserializer deserializer]deserialize:data error:&error];
案列:
TouchJSON可以把对象转化为JSON:
#import "CJSONSerializer.h"
NSData *jsonData = [[CJSONSerializer serializer] serializeObject:dictionary error:&error];
四.SBJSON
解析方法:
#import "SBJson.h"
SBJsonParser *parser=[[SBJsonParser alloc]init];
NSDictionary *weatherDic=[parser objectWithData:data];
案列:
转自http://my.oschina.net/LouWk/blog/112701
最新文章
- foreach的用法
- HDU2509 Be the Winner
- BeagleBone Black– 智能家居控制系统 LAS - ESP8266 UDP 服务
- --@angularJS--浅谈class与Ng-Class的应用
- 跨专业学习编程的苦逼生活 QWQ嘤嘤嘤
- 【Contest Hunter 5302】金字塔
- PHP导出MySQL数据字典 Summer-Mysql-Dic
- linux系统下键盘按键的重新映射——xmodmap工具和xev工具
- RHEL7 DNS 服务 unbound 测试
- [leetcode shell]194. Transpose File
- MJRefresh原理分析
- C# 通过Exchange server 发送邮件
- sqlmap使用帮助文档(1)
- 将list列表中unicode类型的值转换为字符串类型
- Word中高效输入公式:内嵌公式和Mathtype
- #leetcode刷题之路8-字符串转换整数 (atoi)
- HDU2031 进制转换
- 新建maven工程index.jsp页面报错
- No module named appium
- Java探索之旅(15)——包装类和字符类