沈阳专业做网站公司,网站建设公司运营经验,精准数据营销,WordPress头像ssl一个Json解析库#xff0c;其特点是代码简单#xff0c;只有一个.h和.m文件。https://github.com/johnezang/JSONKitJSON(JavaScript Object Notation)是一个轻量级的#xff0c;基于文本的#xff0c;序列结构化数据格式. 由RFC 4627定义.提供以下主要类型#xff1a;nul…一个Json解析库其特点是代码简单只有一个.h和.m文件。https://github.com/johnezang/JSONKitJSON(JavaScript Object Notation)是一个轻量级的基于文本的序列结构化数据格式. 由RFC 4627定义.提供以下主要类型nullBooleantrue and falseNumberStringArrayObject (a.k.a. Associative Arrays, Key/ Value Hash Tables, Maps, Dictionaries, etc.)对应的Objective—C的类JSONObjective-Ctrue and false1、使用NSString *jsonstring [{\age\:18,\book\:{\price\:23.2,\title\:\boook111\},\name\:\samyou\},{\age\:22,\book\:{\price\:33,\title\:\booook222\},\name\:\samsam\}];NSData*data[jsonstring dataUsingEncoding:NSUTF8StringEncoding];NSArray*arr(NSArray *)[data mutableObjectFromJSONData];NSLog(count%d,arr.count);for(int i0;i{NSDictionary*people[arr objectAtIndex:i];NSString*name[people objectForKey:name];NSString*age[people objectForKey:age];NSLog(person withname%,age%d,name,[age intValue]);NSDictionary*book[people objectForKey:book];NSString*bookname[book objectForKey:title];NSNumber*price[book objectForKey:price];NSLog(book with title%,price%f,bookname,[price doubleValue]);}使用上很简单,利用字典键值对获取就行参考http://stephen830.iteye.com/blog/1718550。2、原理JSONKit的源码只有个两个文件先看看解析JSON的流程。NSArray *arr(NSArray *)[data mutableObjectFromJSONData];这个方法是NSData的Category,里面调用了JSONDecoder的解析方法[JSONDecoder decoderWithParseOptions:parseOptionFlags]) mutableObjectWithData:self error:error];总的来说JSONKit对NSArrayNSStringNSDataNSDictionary都进行了序列化和反序列化的扩展可以把这些对象与JSON对象之间相互转换。由于JSONKit没有使用ARC所以使用时不要忘了在build phases -》compile sources 选择文件后面加-fno-objc-arc参数。