您的位置:首页 > Web前端 > JavaScript

JSON之解析通过TouchJSON\SBJSON\JSONKit\NSJSONSeriliz...

2015-06-21 16:43 621 查看
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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: