数据解析之json解析

得到json文件,并且转换为data类型

//step1:文件路径
    NSString *jsonPath = [[NSBundle mainBundle]pathForResource:@"MovieList" ofType:@"txt"];
//step2:转化为nsdata类型 
    NSData *jsondata = [NSData dataWithContentsOfFile:jsonPath];
    //解析json数据  
    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingAllowFragments error:nil];

将Foundtion框架下的对象(一般包括字典和数组)转化为json串

if ([NSJSONSerialization isValidJSONObject:resultDic]) {
            NSData *strData = [NSJSONSerialization dataWithJSONObject:resultDic options:NSJSONWritingPrettyPrinted error:nil];
            //将data转换为字符串
            NSString *Printstr = [[NSString alloc]initWithData:strData encoding:NSUTF8StringEncoding];
            NSLog(@"%@",Printstr);
        } 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文...
    Zcocoa阅读 824评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,868评论 18 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 解析一下.json文档 声明一个student 类.建立.h 和.m 文件 使用系统提供的类来进行 json 解析...
    箫声_筱昇阅读 329评论 0 1
  • 愿你平安归来, 愿你毫发无伤, 愿你的眼睛只看得到笑容, 愿你的梦不会一场空, 愿今后有人陪你颠沛流离 愿你成为自...
    冷渃凕浠阅读 300评论 0 0