MJExtension字典轉模型

本文代碼下載地址

復雜的字典 --> 模型 (模型里面嵌套數組,數組中又嵌套了模型)

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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容