数据解析之json解析

2016-04-13  本文已影响0人  Zzzzzzz7

得到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);
        } 
上一篇 下一篇

猜你喜欢

热点阅读