有时候我们需要读取本地的Json数据,如下:
NSString
*dataPath = [[NSBundle
mainBundle]
pathForResource:@"Questionnaire"
ofType:@"json"];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:dataPath];
NSError *error;
NSArray *questionnaireArr = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
for (NSArray *array in questionnaireArr) {
NSMutableArray *temp = [NSMutableArray array];
for (NSDictionary *dict in array) {
XZQuestionnaireModel *modelQues = [[XZQuestionnaireModel alloc] init];
[modelQues setValuesForKeysWithDictionary:dict];
[temp addObject:modelQues];
}
[self.arrQuestionnaire addObject:temp];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:dataPath];
NSError *error;
NSArray *questionnaireArr = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
for (NSArray *array in questionnaireArr) {
NSMutableArray *temp = [NSMutableArray array];
for (NSDictionary *dict in array) {
XZQuestionnaireModel *modelQues = [[XZQuestionnaireModel alloc] init];
[modelQues setValuesForKeysWithDictionary:dict];
[temp addObject:modelQues];
}
[self.arrQuestionnaire addObject:temp];
}
本文介绍了一种在iOS应用中从本地文件加载JSON数据的方法,并展示了如何解析这些数据到自定义模型类。
2万+

被折叠的 条评论
为什么被折叠?



