iOSios 知识点

MJExtension字典转模型

2016-12-29  本文已影响43人  雷鸣1010

本文代码下载地址

复杂的字典 --> 模型 (模型里面嵌套数组,数组中又嵌套了模型)

json数据如下:

{ 
   "statuses": [
                 {
                 "created_at": "Tue May 31 17:46:55 +0800 2011",
                 "id": 11488058246,
                 "annotations": [{"name" : "北京"},{"name" : "广州"}],
                 "user": {
                 "id": 1404376560,
                 "location": "北京 朝阳区",
                 "description": "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。"
                 }
                 },
                 {
                 "created_at": "Tue May 20 17:46:55 +0800 2016",
                 "id": 12088058246,
                 "annotations": [{"name" : "神武"},{"name" : "武汉"}],
                 "user": {
                 "id": 1104376560,
                 "location": "北京 群众",
                 "description": "人生100年"
                 }
                 }
                 ],
    "ad": [
           {
           "id": 3366614911580000,
           "mark": "AB21321XDXXXX"
           },
           {
           "id": 3366614911586452,
           "mark": "AB21321XDOOOO"
           }
           ],
    "previous_cursor": 0,
    "next_cursor": 11488013766,
    "total_number": 81655
}

(1)首先最外层是一个字典,先拿到这个字典,代码如下:

    NSString *path = [[NSBundle mainBundle]pathForResource:@"weibo" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

(2)创建模型CZWeibo,里面包含最外层的属性,代码如下:


@interface CZWeibo : NSObject
@property(nonatomic,strong)NSArray *statuses;
@property(nonatomic,strong)NSArray *ad;
@property(nonatomic,strong)NSNumber *previous_cursor;
@property(nonatomic,strong)NSNumber *next_cursor;
@property(nonatomic,strong)NSNumber *total_number;
@end

----------------------------------------------------------
@implementation CZWeibo
/**
 *  数组中需要转换的模型类
 *
 *  @return 字典中的key是数组属性名,value是数组中存放模型的Class(Class类型或者NSString类型)
 */

+ (NSDictionary *)mj_objectClassInArray{ 
    return @{@"statuses":[CZStatus class],@"ad":[CZAd class]};
}
@end

(3)创建CZWeibo里面包含的模型CZStatus 和CZAd,里面包含各自的属性,代码如下:

@interface CZStatus : NSObject
@property(nonatomic,copy)NSString *created_at;
@property(nonatomic,copy)NSNumber *idStr;
@property(nonatomic,copy)NSArray *annotations;
@property(nonatomic,copy)CZUser *user;
@end
----------------------------------------------------------
@implementation CZStatus
/**
 *  数组中需要转换的模型类
 *
 *  @return 字典中的key是数组属性名,value是数组中存放模型的Class(Class类型或者NSString类型)
 */

+ (NSDictionary *)mj_objectClassInArray{
    
    return @{@"annotations":[CZUser class]};
}

/**
 *  将属性名换为其他key去字典中取值
 *
 *  @return 字典中的key是属性名,value是从字典中取值用的key,这里只有在字典
内的key值不方便作为属性名时使用
 */
+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    
    return @{@"idStr":@"id"};    
}

@end

@interface CZAd : NSObject
@property(nonatomic,strong)NSNumber *idStr;
@property(nonatomic,copy)NSString *mark;
@end

@implementation CZAd
@end

(4)创建CZStatus里面包含的模型CZUser,里面包含各自的属性,代码如下:



@interface CZUser : NSObject
@property (nonatomic, strong) NSNumber *idStr;  //注意 以后要处理一下  idStr -> id
@property (nonatomic, copy) NSString *location;
@property (nonatomic, copy) NSString *descriptionStr; // //注意 以后要处理一下  descriptionStr -> description

@end


@implementation CZUser
/**
 *  将属性名换为其他key去字典中取值
 *
 *  @return 字典中的key是属性名,value是从字典中取值用的key,
这里只有在字典内的key值不方便作为属性名时使用,(例如key值是description时,
如果使用description作为属性进行命名,使用.语法时容易调者方法,所以这里可以
使用descriptionStr代替)
 */
+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
    return @{@"idStr" : @"id",@"descriptionStr":@"description"};
}
@end
上一篇下一篇

猜你喜欢

热点阅读