iphone开发之SBJSON解析(的使用)
2012-09-04 21:27
260 查看
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 ivar else if ([jsonObject isKindOfClass:[NSArray class]]) // treat as an array or reassign to an array ivar.
转换代码: 如果直接使用 NSMutableDictionary *root 来获得 字符串,将会对json字串产生以下变化
1. 中文问题
2.加入了()符号
3.所有的: 变成了 =
要避免这个问题的产生,需要用
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSString *jsonString = [jsonWriter stringWithObject:root];
[jsonWriter release];
这段代码来把 NSMutableDictionary 转成 NSString
SBJson的项目官网,以及源文件下载 https://github.com/stig/json-framework/downloads
xcode4.2.1 中使用 sbjson 的 3.1版本来解析json字符串:
http://blog.csdn.net/remote_roamer/article/details/7021640
sbjson的用法(翻译):
http://www.devdiv.com/iOS_iPhone-sbjson%E7%9A%84%E7%94%A8%E6%B3%95%EF%BC%88%E7%BF%BB%E8%AF%91%EF%BC%89-thread-122618-1-1.html
相关文章推荐
- iphone开发 使用TouchJSON框架 解析JSON
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
- iphone开发 使用TouchJSON框架 解析JSON
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
- iphone开发之使用touchXml解析xml并且存储到SQLite中
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
- iPhone开发【十九】XML解析之NSXMLParser(使用Web Services查询火车信息)
- iPhone开发【十九】XML解析之NSXMLParser(使用Web Services查询火车信息)
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
- iOS Xcode7.2开发时使用SBJson解析时 出现野指针的问题解决
- iPhone开发【十九】XML解析之NSXMLParser(使用Web Services查询火车信息)
- ios 中使用SBJson拼接和解析json
- iPhone网络开发之如何使用NSURLConnection
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境
- iphone开发之线程的使用详解
- 【iphone游戏开发】iphone-Cocos2D游戏开发之二:精灵表的详细讲解(一)和Zwoptex工具的使用