JSON解析1:使用系統(tǒng)自帶的解析
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
//使用系統(tǒng)自帶的JSON解析
- (IBAction)JsonSystem:(UIButton *)sender {
//1獲取文件路徑
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"message.txt" ofType:nil];
//2創(chuàng)建date接收對(duì)象
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
//3使用系統(tǒng)提供的JSAON類堤框,將需要解析的文件傳入藻茂,由于最外側(cè)是數(shù)組蕊肥,所以最終解析的數(shù)據(jù)應(yīng)該由數(shù)組接收
NSArray *temArray = [NSJSONSerialization JSONObjectWithData:fileData options:(NSJSONReadingAllowFragments)error:nil];
NSLog(@"%@",temArray);
//初始化數(shù)組
self.dateArray = [NSMutableArray array];
//3.1 for in
for ( NSDictionary *dict in temArray) {
NSLog(@"====%@",dict[@"receiver"]);
//3.2創(chuàng)建模型對(duì)象
Message *message = [Message new];
[message setValuesForKeysWithDictionary:dict];
NSLog(@"++++++%@",message.receiver);
//3.3將模型數(shù)據(jù)的東西放到數(shù)組內(nèi)部
[self.dateArray addObject:message];
}
for (NSString *str in self.dateArray) {
NSLog(@"------%@",str);
}
//測(cè)試打印
[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