加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SBJson的使用

发布时间:2020-12-16 19:41:04 所属栏目:百科 来源:网络整理
导读:转自:http://blog.csdn.net/binyanye1/article/details/8632770 1,访问 SBJson的项目官网,并且下载https://github.com/stig/json-framework/downloads 注意 :按照作者的说明, SBJson v3.1alpha3 - source and API docs for Mac and iOS development,now

转自:http://blog.csdn.net/binyanye1/article/details/8632770

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

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


控制台打印的内容如下:

2011-11-29 02:56:04.882 IManager[4040:fb03] {
sex = "U7537";
userName = "U5f90U6cfdU5b87";
}
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"}]}

copy
    +(void)test{
  1. DLog(@"test开始运行");
  2. NSString*customerGridJsonString=[[NSStringalloc]initWithString:@"{"customer":[{"name":"roamer","ycount":"232.4","sumcount":"322.3"},{"name":"王三","ycount":"221.2","sumcount":"1123.2"},{"name":"李四","ycount":"1221.2","sumcount":"12123.2"}]}"];
  3. DLog(@"%@",customerGridJsonString);
  4. NSError*error=nil;
  5. NSMutableDictionary*root=[[NSMutableDictionaryalloc]initWithDictionary:[parserobjectWithString:customerGridJsonStringerror:&error]];
  6. //注意转换代码
  7. SBJsonWriter*jsonWriter=[[SBJsonWriteralloc]init];
  8. NSString*jsonString=[jsonWriterstringWithObject:root];
  9. [jsonWriterrelease];
  10. //注意转换代码
  11. NSMutableArray*customers=[rootobjectForKey:@"customer"];
  12. DLog(@"%@",customers);
  13. for(NSMutableDictionary*memberincustomers)
  14. "name"]description]);
  15. }

  16. 控制台输出是:

    2012-04-22 13:55:41.845 iQuestionnaire[13464:fb03] test开始运行

    2012-04-22 13:55:41.846 iQuestionnaire[13464:fb03] {"customer":[{"name":"roamer",{"name":"王三",{"name":"李四","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 = "U738bU4e09";

    sumcount = "1123.2";

    ycount = "221.2";

    name = "U674eU56db";

    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 = [[SBJsonWriteralloc]init];

    NSString*jsonString = [jsonWriterstringWithObject:root];

    [jsonWriterrelease];


    这段代码来把NSMutableDictionary 转成 NSString

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读