OC-数据解析JSON解析(四)

JSON解析2:使用第三方框架(JSONKit)

#import "ViewController.h"

#import "Message.h"

#import "JSONKit.h"

@interface ViewController ()

- (IBAction)jisonKti:(UIButton *)sender;

//存储模型对象

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation ViewController

- (IBAction)jisonKti:(UIButton *)sender {

//1获取文件路径

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"message.txt" ofType:nil];

//转化

NSData *fileData = [NSData dataWithContentsOfFile:filePath];

NSArray *temArray = [fileData objectFromJSONData];

NSLog(@"%@",temArray);

//初始化数组,用来接收模型对象

self.dataArray = [NSMutableArray array];

//遍历数组,取出字典。然后通过KVC将字典当中元素提取,在放入self.dataArray中

for (NSDictionary *dict in temArray) {

//创建模型对象

Message *message = [Message new];

//通过kvc将字典转化为模型对象

[message setValuesForKeysWithDictionary:dict];

//将模型对象放入数组中

[self.dataArray addObject:message];

//测试

[self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSLog(@"%@ ,%@,%@,$@",[obj content],[obj sender],[obj receiver],[obj date]);

}];

}

}

//源文件百度云:链接: https://pan.baidu.com/s/1qYmCTRm 密码: d3vy

//案例链接: https://pan.baidu.com/s/1mhNc9ao 密码: 7fbz

你可能感兴趣的:(OC-数据解析JSON解析(四))