cocoa 下json开源的类库有很多,其中JSONKit库是非常简单易用而且效率又比较高的。
想要使用JSONKit库来解析json文件,只需要下载JSONKit.h 和JSONKit.m添加到工程中(下载链接);然后加入libz.dylib即可
解析代码举例:
#import "JSONKit.h" //假设 strJson 是网络上接收到的 json 字符串, NSString *strJson = @"{\"aps\: {\"alert\":{\"body\":\"a msg come!\"},\"bage\":3,\"sound\":\"def.mp3\"}}"; NSDictionary *result = [jsonData objectFromJSONData];
字典result便是解析好的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:3bage" ]; [aps setObject:def.mp3sound"]; [jsonDic setObject:aps forKey:aps"]; NSString *strJson = [jsonDic JSONString];