得到j(luò)son文件类少,并且轉(zhuǎn)換為data類(lèi)型
//step1:文件路徑
NSString *jsonPath = [[NSBundle mainBundle]pathForResource:@"MovieList" ofType:@"txt"];
//step2:轉(zhuǎn)化為nsdata類(lèi)型
NSData *jsondata = [NSData dataWithContentsOfFile:jsonPath];
//解析json數(shù)據(jù)
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingAllowFragments error:nil];
將Foundtion框架下的對(duì)象(一般包括字典和數(shù)組)轉(zhuǎn)化為json串
if ([NSJSONSerialization isValidJSONObject:resultDic]) {
NSData *strData = [NSJSONSerialization dataWithJSONObject:resultDic options:NSJSONWritingPrettyPrinted error:nil];
//將data轉(zhuǎn)換為字符串
NSString *Printstr = [[NSString alloc]initWithData:strData encoding:NSUTF8StringEncoding];
NSLog(@"%@",Printstr);
}