得到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);
}
网友评论