MJExtension字典轉(zhuǎn)模型

本文代碼下載地址

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

json數(shù)據(jù)如下:

{ 
   "statuses": [
                 {
                 "created_at": "Tue May 31 17:46:55 +0800 2011",
                 "id": 11488058246,
                 "annotations": [{"name" : "北京"},{"name" : "廣州"}],
                 "user": {
                 "id": 1404376560,
                 "location": "北京 朝陽區(qū)",
                 "description": "人生五十年,乃如夢如幻;有生斯有死,壯士復(fù)何憾。"
                 }
                 },
                 {
                 "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)創(chuàng)建模型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
/**
 *  數(shù)組中需要轉(zhuǎn)換的模型類
 *
 *  @return 字典中的key是數(shù)組屬性名,value是數(shù)組中存放模型的Class(Class類型或者NSString類型)
 */

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

(3)創(chuàng)建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
/**
 *  數(shù)組中需要轉(zhuǎn)換的模型類
 *
 *  @return 字典中的key是數(shù)組屬性名,value是數(shù)組中存放模型的Class(Class類型或者NSString類型)
 */

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

/**
 *  將屬性名換為其他key去字典中取值
 *
 *  @return 字典中的key是屬性名,value是從字典中取值用的key,這里只有在字典
內(nèi)的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)創(chuàng)建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,
這里只有在字典內(nèi)的key值不方便作為屬性名時使用,(例如key值是description時,
如果使用description作為屬性進行命名,使用.語法時容易調(diào)者方法,所以這里可以
使用descriptionStr代替)
 */
+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
    return @{@"idStr" : @"id",@"descriptionStr":@"description"};
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容