JSON解析1:使用系统自带的解析
message.txt
// ViewController.m
// Json(System)解析
//
//
#import "ViewController.h"
#import "Message.h"
@interface ViewController ()
- (IBAction)JsonSystem:(UIButton *)sender;
@property(nonatomic,strong)NSMutableArray *dateArray;
@end
@implementation ViewController
//使用系统自带的JSON解析
- (IBAction)JsonSystem:(UIButton *)sender {
//1获取文件路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"message.txt" ofType:nil];
//2创建date接收对象
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
//3使用系统提供的JSAON类,将需要解析的文件传入,由于最外侧是数组,所以最终解析的数据应该由数组接收
NSArray *temArray = [NSJSONSerialization JSONObjectWithData:fileData options:(NSJSONReadingAllowFragments)error:nil];
NSLog(@"%@",temArray);
//初始化数组
self.dateArray = [NSMutableArray array];
//3.1 for in
for ( NSDictionary *dict in temArray) {
NSLog(@"====%@",dict[@"receiver"]);
//3.2创建模型对象
Message *message = [Message new];
[message setValuesForKeysWithDictionary:dict];
NSLog(@"++++++%@",message.receiver);
//3.3将模型数据的东西放到数组内部
[self.dateArray addObject:message];
}
for (NSString *str in self.dateArray) {
NSLog(@"------%@",str);
}
//测试打印
[self.dateArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@ %@ %@ %@",[obj content],[obj receiver],[obj date],[obj sender]);
}];
}
//源文件百度云:链接: https://pan.baidu.com/s/1gfqZbhT 密码: hkbn
网友评论