美文网首页
MJExtension 解析嵌套model

MJExtension 解析嵌套model

作者: Victory_886 | 来源:发表于2019-04-04 23:34 被阅读0次

MJ 的使用

直接上代码吧

NewsViewController.m 文件


- (void)viewDidLoad  {
    [super viewDidLoad];
    self.navigationItem.title = @"新闻";
    
    [self test_mj];
}

- (void)test_mj {

    // 自定义的json数据
    NSDictionary *dict = @{
                           @"resut":@{
                                   @"name":@"terry",
                                   @"age":@"21",
                                   @"info":@[
                                           @{@"a1":@"a1",@"a2":@"a2",@"my":@{@"n1":@"n1",@"n2":@[@{@"m1":@"m1",@"m2":@"m21"}]}},
                                           @{@"a1":@"b1",@"a2":@"b2",@"my":@{@"n1":@"n2",@"n2":@[@{@"m1":@"m2",@"m2":@"m22"}]}},
                                           @{@"a1":@"c1",@"a2":@"c2",@"my":@{@"n1":@"n3",@"n2":@[@{@"m1":@"m3",@"m2":@"m23"}]}},
                                           ],
                                   },
                           @"code":@"1",
                           @"msg":@"",
                           };

    TestModel *model = [TestModel mj_objectWithKeyValues:dict[@"resut"]];
    for (infoModel *infoModel in model.info) {
        
        NSLog(@"info-a1 %@",infoModel.a1);
        NSLog(@"info-my-n1 %@",infoModel.my.n1);
        
        for (n2Model *n2Model in infoModel.my.n2) {
            
            NSLog(@"info-my-n2-m1 %@",n2Model.m1);
        }
    }
}

TestModel.h 文件


@interface n2Model : NSObject

@property (nonatomic, copy) NSString *m1;
@property (nonatomic, copy) NSString *m2;

@end

@interface myModel : NSObject

@property (nonatomic, copy) NSString                *n1;
@property (nonatomic, strong) NSArray <n2Model *>   *n2;

@end

@interface infoModel : NSObject

@property (nonatomic, copy) NSString    *a1;
@property (nonatomic, copy) NSString    *a2;
@property (nonatomic, copy) NSString    *a3;
@property (nonatomic, strong) myModel   *my;


@end

@interface TestModel : NSObject

@property (nonatomic, copy) NSString                *name;
@property (nonatomic, copy) NSString                *age;
@property (nonatomic, strong) NSArray<infoModel *>  *info;

@end

TestModel.m 文件

#import "TestModel.h"

@implementation TestModel

+ (NSDictionary *)mj_objectClassInArray {
    
    return @{@"info" : @"infoModel"};  //前边,是属性数组的名字,后边就是类名
}

@end


@implementation infoModel

+ (NSDictionary *)mj_objectClassInArray {
    
    return @{@"my" : @"myModel"};  //前边,是属性数组的名字,后边就是类名
}

@end



@implementation myModel

+ (NSDictionary *)mj_objectClassInArray {
    
    return @{@"n2" : @"n2Model"};  //前边,是属性数组的名字,后边就是类名
}

@end



@implementation n2Model

@end


相关文章

网友评论

      本文标题:MJExtension 解析嵌套model

      本文链接:https://www.haomeiwen.com/subject/gqbciqtx.html