iOS巅峰之SBJSON解析
来源:互联网 发布:全球最出名的程序员 编辑:程序博客网 时间:2024/03/28 23:09
JSON是一种数据交换语言,和XML是同样用途的。不过JSON的体积要比XML小,也就意味着在网络传输中速度会比XML更快。
这里可以看到更多关于json的资料:http://www.json.org/json-zh.html
其实说白了 JSON无非就是一种字符串的结构。。。 那么在IPHONE开发中,使用最多的JSON解析库就是 SBJson。。。
在这里可以得到 SBJson:http://stig.github.com/json-framework/
3.1版本及以上,它将支持ARC。下载下来后,解压缩,然后把Class目录下的所有文件导入项目中。
#import "SBJson.h"
- NSString *jsonStr = @"{\"name\":\"jia\",\"age\":\"24\"}";
- NSString *jsonStr2 = @"[\"1\",\"2\"]";
- SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
- NSMutableDictionary *dict = [jsonParser objectWithString:jsonStr];
- NSLog(@"%@",dict);
- NSMutableArray *arr = [jsonParser objectWithString:jsonStr2];
- NSLog(@"%@",arr);
- [jsonParser release];
可以看到,无论是数组还是字典,都使用
- objectWithString
- 来解析。
///----------------------------------
bjson有一个有用的sbjsonparser类,可以在一行内搞定整个json串的解析:
SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; NSError *error = nil; NSArray *jsonObjects = [jsonParser objectWithString:jsonString error:&error]; [jsonParser release], jsonParser = nil;
sbjson将json作为objective-c的字典来处理。对于不同的webservice,你可能得到一个顶级的json对象或者一个数组。因此,objectWithString:error:有一个id类型返回值。你可以使用objective-c的动态特性来决定解析返回时采用何种数据来接收,如下:
id jsonObject = [jsonParser objectWithString:jsonString error:&error]; if ([jsonObject isKindOfClass:[NSDictionary class]]) // treat as a dictionary, or reassign to a dictionary ivarelse if ([jsonObject isKindOfClass:[NSArray class]]) // treat as an array or reassign to an array ivar.
0 0
- iOS巅峰之SBJSON解析
- iOS json解析之SBJson
- ios开发之解析json字符串(SBJson)
- iOS SBJson网络解析
- iphone 之SBJson 解析
- iOS巅峰之NSRunLoop
- iOS之SBJson的使用
- iOS 使用SBJSON创建和解析JSON
- iOS 使用SBJSON创建和解析JSON
- SBJson解析
- ios巅峰之SQ数据库
- IOS巅峰之UICollectionView详解
- iOS巅峰之跑马灯
- iOS巅峰之label描边
- iOS SBJson解析,拼接的用法
- ios 中使用SBJson拼接和解析json
- iOS SBJSON解析错误 Unescaped control character [0x09]
- ios 中使用SBJson拼接和解析json
- android 入门记录
- 文章标题
- JDBC典型应用-存储过程
- eclipse集成maven3后,创建java项目详细图解
- 三种实现iOS瀑布流的思路总结
- iOS巅峰之SBJSON解析
- imageNamed 与 imageWithContentsOfFile的区别
- IOS流媒体播放器V1.1_rtsp、rmtp、m3u8、mms、mmsh
- vitamio播放网络音频的折腾
- SlidingMenu_崩溃的BUG_1
- 关于oracle upgrade与migration的文档
- 广告设计首页
- useradd 与adduser的区别
- C++primer第五版第十一章学习笔记