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

 

posted on 2013-07-05 14:22  一梦浮生2012  阅读(448)  评论(0编辑  收藏  举报