网络:YYModel 使用(JSON到模型的转换)

来源:互联网 发布:电脑找不到wifi网络 编辑:程序博客网 时间:2024/05/16 13:39
#import "ViewController.h"#import "YYModel.h"#import "Person.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    // 有时候一个json可能返回四五个模型的数据    // 需要判断是哪个key//    //    NSDictionary *dict = @{//                           @"name":@"张三",//                           @"age":@20,//                           @"id":@"12345",//                           @"book":@{//                                            @"name":@"iOS开发"//                                          //                                            }//                           };//    //    Person *person = [Person yy_modelWithJSON:dict];//    //    //    NSLog(@"%@",person);    // 加载数据    NSString *path = [[NSBundle mainBundle]pathForResource:@"data.json" ofType:nil];    NSData *data = [NSData dataWithContentsOfFile:path];    NSArray *users = [NSArray yy_modelArrayWithClass:[Person class] json:data];    // 转成 数组    // 遍历数组    // 创建一个可变数组    // 创建一个模型    // 判断如果key 是 book  再来判断一个book模型    NSLog(@"%@",users);}@end
0 0
原创粉丝点击