框架下载地址:https://github.com/TouchCode/TouchJSON
然后是所谓的配置:
1.把下载解压后的目录下的Source拖进工程;
2.引入:
a.解析的话,#import “CJSONDeserializer.h”
b.生成的话,#import “CJSONSerializer.h”
最后是用法,不完全api.
a.解析=====================
// 生成解析器
+ (CJSONDeserializer *)deserializer;
// 解析成Dictory or Array
- (id)deserialize:(NSData *)inData error:(NSError **)outError;
b.创建=====================
// 生成翻译器
+ (CJSONSerializer *)serializer;
// 检查json是否合法
- (BOOL)isValidJSONObject:(id)inObject;
//创建JSON对应的data
- (NSData *)serializeObject:(id)inObject error:(NSError **)outError;
最最后,举例.
直接看代码吧
NSString *jsonStringSrc = @"从文件读取或者从网络获得的JSON字符串"; NSData *jsonData = [jsonStringSrc dataUsingEncoding:NSUTF32BigEndianStringEncoding]; NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil]; NSLog(@"%@",dictionary); // Create NSDictionary *dictionarySrc = [NSDictionary dictionaryWithObject:@"b" forKey:@"a"]; jsonData = [[CJSONSerializer serializer] serializeObject:dictionarySrc error:nil]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF32BigEndianStringEn coding]; NSLog(@"%@",jsonString);
附上原工程,Demo.