xcode4.2.1 中使用 sbjson 的 3.1版本来解析json字符串
2011-11-29 03:03
936 查看
1,访问 SBJson的项目官网,并且下载 https://github.com/stig/json-framework/downloads
注意:按照作者的说明,
2.依然采用源代码编译的方式。把SBJson下载后解开的 目录中的classes目录拖拉到 项目中。
3.在项目的h文件中引入#import "SBJson.h"。不再使用#import "JSON.h"
4.测试用的json字符串是: {"userInfo":{"userName":"徐泽宇","sex":"男"}}
5.测试代码是 :
控制台打印的内容如下:
2011-11-29 02:56:04.882 IManager[4040:fb03] {
sex = "\U7537";
userName = "\U5f90\U6cfd\U5b87";
}
2011-11-29 02:56:04.887 IManager[4040:fb03] 徐泽宇
2011-11-29 02:56:04.888 IManager[4040:fb03] 男
注意:这里的json的字符串。一定要用双引号,不能使用单引号。这点上和java的类包是有区别的。这个问题浪费了我1个小时。
处理json对象有多个记录的方法。
json字符串是:
{"customer":[{"name":"roamer","ycount":"232.4","sumcount":"322.3"},{"name":"王三","ycount":"221.2","sumcount":"1123.2"},{"name":"李四","ycount":"1221.2","sumcount":"12123.2"}]}
控制台输出是:
2012-04-22 13:55:41.845 iQuestionnaire[13464:fb03] test
开始运行
2012-04-22 13:55:41.846 iQuestionnaire[13464:fb03] {"customer":[{"name":"roamer","ycount":"232.4","sumcount":"322.3"},{"name":"王三","ycount":"221.2","sumcount":"1123.2"},{"name":"李四","ycount":"1221.2","sumcount":"12123.2"}]}
2012-04-22 13:55:41.854 iQuestionnaire[13464:fb03] {"customer":[{"sumcount":"322.3","name":"roamer","ycount":"232.4"},{"sumcount":"1123.2","name":"王三","ycount":"221.2"},{"sumcount":"12123.2","name":"李四","ycount":"1221.2"}]}
[
2012-04-22 13:55:41.854 iQuestionnaire[13464:fb03] (
{
name = roamer;
sumcount = "322.3";
ycount = "232.4";
},
{
name = "\U738b\U4e09";
sumcount = "1123.2";
ycount = "221.2";
},
{
name = "\U674e\U56db";
sumcount = "12123.2";
ycount = "1221.2";
}
)
2012-04-22 13:55:41.854 iQuestionnaire[13464:fb03] roamer
2012-04-22 13:55:41.856 iQuestionnaire[13464:fb03]
王三
2012-04-22 13:55:41.856 iQuestionnaire[13464:fb03]
李四
注意这里的转换代码: 如果直接使用
NSMutableDictionary *root 来获得 字符串,将会对json字串产生以下变化
1. 中文问题
2.加入了()符号
3.所有的: 变成了 =
要避免这个问题的产生,需要用
SBJsonWriter *jsonWriter = [[SBJsonWriter
alloc] init];
NSString *jsonString = [jsonWriter
stringWithObject:root];
[jsonWriter release];
这段代码来把 NSMutableDictionary 转成 NSString
注意:按照作者的说明,
SBJson v3.1alpha3 - source and API docs for Mac and iOS development, now with ARC support!
只有在3.1的版本上,才支持xcode 4.2中开启ARC的功能。所以我使用这个版本。2.依然采用源代码编译的方式。把SBJson下载后解开的 目录中的classes目录拖拉到 项目中。
3.在项目的h文件中引入#import "SBJson.h"。不再使用#import "JSON.h"
4.测试用的json字符串是: {"userInfo":{"userName":"徐泽宇","sex":"男"}}
5.测试代码是 :
//测试json的解析 -(void)testJsonParser: (NSString *) jsonString { jsonString = [[NSString alloc] initWithString:@"{\"userInfo\":{\"userName\":\"徐泽宇\",\"sex\":\"男\"}}"]; NSLog(@"正在解析json字符串是:%@",jsonString); SBJsonParser * parser = [[SBJsonParser alloc] init]; NSError * error = nil; NSMutableDictionary *jsonDic = [parser objectWithString:jsonString error:&error]; NSMutableDictionary * dicUserInfo = [jsonDic objectForKey:@"userInfo"]; NSLog(@"%@",[jsonDic objectForKey:@"userInfo" ]); NSLog(@"%@",[dicUserInfo objectForKey:@"userName"]); NSLog(@"%@",[dicUserInfo objectForKey:@"sex"]); }
控制台打印的内容如下:
2011-11-29 02:56:04.882 IManager[4040:fb03] {
sex = "\U7537";
userName = "\U5f90\U6cfd\U5b87";
}
2011-11-29 02:56:04.887 IManager[4040:fb03] 徐泽宇
2011-11-29 02:56:04.888 IManager[4040:fb03] 男
注意:这里的json的字符串。一定要用双引号,不能使用单引号。这点上和java的类包是有区别的。这个问题浪费了我1个小时。
处理json对象有多个记录的方法。
json字符串是:
{"customer":[{"name":"roamer","ycount":"232.4","sumcount":"322.3"},{"name":"王三","ycount":"221.2","sumcount":"1123.2"},{"name":"李四","ycount":"1221.2","sumcount":"12123.2"}]}
+(void) test{ DLog(@"test 开始运行"); NSString * customerGridJsonString = [[NSString alloc]initWithString:@"{\"customer\":[{\"name\":\"roamer\",\"ycount\":\"232.4\",\"sumcount\":\"322.3\"},{\"name\":\"王三\",\"ycount\":\"221.2\",\"sumcount\":\"1123.2\"},{\"name\":\"李四\",\"ycount\":\"1221.2\",\"sumcount\":\"12123.2\"}]}"]; SBJsonParser * parser = [[SBJsonParser alloc] init]; DLog(@"%@",customerGridJsonString); NSError * error = nil; NSMutableDictionary *root = [[NSMutableDictionary alloc] initWithDictionary:[parser objectWithString:customerGridJsonString error:&error]]; //注意转换代码 SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init]; NSString *jsonString = [jsonWriter stringWithObject:root]; [jsonWriter release]; DLog(@"%@",jsonString); //注意转换代码 NSMutableArray * customers = [root objectForKey:@"customer"]; DLog(@"%@",customers); for(NSMutableDictionary * member in customers) { DLog(@"%@",[[member objectForKey:@"name"] description]); } }
控制台输出是:
2012-04-22 13:55:41.845 iQuestionnaire[13464:fb03] test
开始运行
2012-04-22 13:55:41.846 iQuestionnaire[13464:fb03] {"customer":[{"name":"roamer","ycount":"232.4","sumcount":"322.3"},{"name":"王三","ycount":"221.2","sumcount":"1123.2"},{"name":"李四","ycount":"1221.2","sumcount":"12123.2"}]}
2012-04-22 13:55:41.854 iQuestionnaire[13464:fb03] {"customer":[{"sumcount":"322.3","name":"roamer","ycount":"232.4"},{"sumcount":"1123.2","name":"王三","ycount":"221.2"},{"sumcount":"12123.2","name":"李四","ycount":"1221.2"}]}
[
2012-04-22 13:55:41.854 iQuestionnaire[13464:fb03] (
{
name = roamer;
sumcount = "322.3";
ycount = "232.4";
},
{
name = "\U738b\U4e09";
sumcount = "1123.2";
ycount = "221.2";
},
{
name = "\U674e\U56db";
sumcount = "12123.2";
ycount = "1221.2";
}
)
2012-04-22 13:55:41.854 iQuestionnaire[13464:fb03] roamer
2012-04-22 13:55:41.856 iQuestionnaire[13464:fb03]
王三
2012-04-22 13:55:41.856 iQuestionnaire[13464:fb03]
李四
注意这里的转换代码: 如果直接使用
NSMutableDictionary *root 来获得 字符串,将会对json字串产生以下变化
1. 中文问题
2.加入了()符号
3.所有的: 变成了 =
要避免这个问题的产生,需要用
SBJsonWriter *jsonWriter = [[SBJsonWriter
alloc] init];
NSString *jsonString = [jsonWriter
stringWithObject:root];
[jsonWriter release];
这段代码来把 NSMutableDictionary 转成 NSString
相关文章推荐
- IOS开发之——使用SBJson拼接Json字符串
- httpclient demo 3.1 4.2不同版本的API实现 解析JSON
- 使用Gson解析常见json字符串
- ios 中使用SBJson拼接和解析json
- 使用js代码解析json字符串
- cocos2d-x 使用libjson解析json--------------cocos2d-x 3.0正式版本(7.6)
- ios 中使用SBJson拼接和解析json
- Ajax使用一+javascript解析Ajax返回的json字符串
- 使用Gson解析json格式的字符串的正确方式
- 工作笔记之复杂字符串的json解析,使用gson
- iOS 使用SBJSON创建和解析JSON
- fasterxml jackson的使用--从pojo转成json字符串和json解析回pojo
- 开发android4.0以上版本网络编程模块的问题 使用URLConnection 怎么连接不上 获取Json字符串
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
- 使用 dynamic 标记解析JSON字符串(转)
- 使用Gson解析json格式的字符串的正确方式
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- 使用Ext.decode()解析JSON字符串时firebug提示extjs missing ] after element list错误
- iOS 使用SBJSON创建和解析JSON
- 使用cJSON解析JSON字符串