JSON解析方法
1.原生:NSJSONSerialization(序列化工具)
2.第三方框架:JSONKit SBJson TouchJson
反序列化(JSON转oc)
//2.JSON ——————》oc对象 反序列化
// NSJSONReadingMutableContainers = (1UL << 0), 可变字典和数组
// NSJSONReadingMutableLeaves = (1UL << 1), 内部所有的字符串都是可变的
// NSJSONReadingAllowFragments = (1UL << 2) 既不是字典也不是数组 则使用该枚举
//参数options:的选择 要看返回的最外层 此处可传0 (kNilOptions) 性能最高
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:nil];
序列化(oc对象转JSON)
NSArray *array = @[@"12553",@"2360"];
//并不是所有的oc对象都可以转化JSON
NSString *str = @"MJR";
//判断是否可以序列化
BOOL isValid = [NSJSONSerialization isValidJSONObject:str];
if (isValid) {
return;
}
//NSJSONWritingPrettyPrinted = (1UL << 0)排版 美观
NSData *data = [NSJSONSerialization dataWithJSONObject:array options:kNilOptions error:0];
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);