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

JSONKit的使用方法

发布时间:2020-12-16 19:42:29 所属栏目:百科 来源:网络整理
导读:#import "JSONKit.h"//假设 strJson 是网络上接收到的 json 字符串,NSString *strJson = @"[{"Id": 1,"BrandName": "爱马仕" },{"Id": 2,"BrandName": "安娜苏"}]"; NSArray *arrlist=[strJson objectFromJSONString];NSLog(@"%d",[arrlist cou
#import "JSONKit.h"

//假设 strJson 是网络上接收到的 json 字符串,
NSString *strJson = @"[{"Id": 1,"BrandName": "爱马仕" },{"Id": 2,"BrandName": "安娜苏"}]"; 
NSArray *arrlist=[strJson objectFromJSONString];
NSLog(@"%d",[arrlist count]);
for (int i=0; i<[arrlist count]; i++) {
NSDictionary *item=[arrlist objectAtIndex:i];
NSString *BrandName=[item objectForKey:@"BrandName"];
NSLog(@"%@",BrandName);
}

json开源的类库有很多,其中JSONKit库是非常简单易用而且效率又比较高的,重要的JSONKit适用于ios 5.0以下的版本。

下载地址: https://github.com/johnezang/JSONKit

使用JSONKit库来解析json文件,只需要下载JSONKit.h 和JSONKit.m添加到工程中;然后加入libz.dylib即可

解析代码举例:

字典arrlist便是解析好的json文件了。

JSONKit库也可以用来生成json文件

代码举例:
NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
NSMutableDictionary *alert = [NSMutableDictionary dictionary]
;NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[alert setObject:@"a msg come!" forKey:@"body"];
[aps setObject:alert forKey:@"alert"];
[aps setObject:@"3" forKey:@"bage" ];
[aps setObject:@"def.mp3" forKey:@"sound"];
[jsonDic setObject:aps forKey:@"aps"];
NSString *strJson = [jsonDic JSONString];

(编辑:李大同)

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

    推荐文章
      热点阅读