您的位置:首页 > Web前端 > JavaScript

JSON解析

2016-12-13 18:19 253 查看
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]);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: