当前位置: 代码迷 >> JavaScript >> [转]xcode4.2.1 中运用 sbjson 的 3.1版本来解析json字符串
  详细解决方案

[转]xcode4.2.1 中运用 sbjson 的 3.1版本来解析json字符串

热度:765   发布时间:2012-09-10 22:20:12.0
[转]xcode4.2.1 中使用 sbjson 的 3.1版本来解析json字符串

http://blog.csdn.net/remote_roamer/article/details/7021640

?

?

1,访问 SBJson的项目官网,并且下载?https://github.com/stig/json-framework/downloads

??? 注意:按照作者的说明,

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.测试代码是 :

view plain
  1. //测试json的解析??
  2. -(void)testJsonParser:?(NSString?*)?jsonString??
  3. {??
  4. ????jsonString?=?[[NSString?alloc]?initWithString:@"{\"userInfo\":{\"userName\":\"徐泽宇\",\"sex\":\"男\"}}"];??
  5. ????NSLog(@"正在解析json字符串是:%@",jsonString);??
  6. ??????
  7. ????SBJsonParser?*?parser?=?[[SBJsonParser?alloc]?init];??
  8. ????NSError?*?error?=?nil;??
  9. ????NSMutableDictionary?*jsonDic?=?[parser?objectWithString:jsonString?error:&error];??
  10. ????NSMutableDictionary?*?dicUserInfo?=?[jsonDic?objectForKey:@"userInfo"];??
  11. ??????
  12. ????NSLog(@"%@",[jsonDic?objectForKey:@"userInfo"?]);??
  13. ????NSLog(@"%@",[dicUserInfo?objectForKey:@"userName"]);??
  14. ????NSLog(@"%@",[dicUserInfo?objectForKey:@"sex"]);??
  15. }??



控制台打印的内容如下:

?

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"}]}

?

?

view plain
  1. [customerGridJsonString?appendString:@"{\"customer\":[{\"name\":\"roamer\",\"ycount\":\"232.4\",\"sumcount\":\"322.3\"},{\"name\":\"王三\",\"ycount\":\"221.2\",\"sumcount\":\"1123.2\"},{\"name\":\"李四\",\"ycount\":\"1221.2\",\"sumcount\":\"12123.2\"}]}"];??
  2. ????SBJsonParser?*?parser?=?[[SBJsonParser?alloc]?init];??
  3. ????NSLog(@"%@",customerGridJsonString);??
  4. ????NSError?*?error?=?nil;??
  5. ??????
  6. ????NSMutableDictionary?*root?=?[[NSMutableDictionary?alloc]?initWithDictionary:[parser?objectWithString:customerGridJsonString?error:&error]];??
  7. ??????
  8. ????NSMutableArray?*?customers?=?[root?objectForKey:@"customer"];??
  9. ??????
  10. ????for(NSMutableDictionary?*?member??in?customers)??
  11. ????{??
  12. ????????NSLog(@"%@",[[member?objectForKey:@"name"]?description]);??
  13. ????}??


控制台输出是:

?

2011-12-01 01:59:15.731 IManager[2922:fb03] {"customer":[{"name":"roamer","ycount":"232.4","sumcount":"322.3"},{"name":"王三","ycount":"221.2","sumcount":"1123.2"},{"name":"李四","ycount":"1221.2","sumcount":"12123.2"}]}
2011-12-01 01:59:15.732 IManager[2922:fb03] roamer
2011-12-01 01:59:15.737 IManager[2922:fb03] 王三
2011-12-01 01:59:15.737 IManager[2922:fb03] 李四

  相关解决方案