JSONModel

開源第三方地址: https://github.com/icanzilb/JSONModel

  • 1 model 創(chuàng)建
#import "JSONModel.h"

// 次model
@interface ScoreModel : JSONModel
@property (nonatomic, strong) NSString *english;
@property (nonatomic, strong) NSString *chinese;
@end

// 主model
@interface UserModel : JSONModel
@property (nonatomic, strong) NSString *name;       // 一般
@property (nonatomic, strong) ScoreModel *scores;  // model 嵌套 model
@property (nonatomic, strong) NSString *eng_score; // 獲取 scores 中的 英語成績,model多級(jí)映射
@property (nonatomic, strong) NSArray *scoreArray;// model 嵌套數(shù)組
@end

  • 2 model 屬性 可空 或 不存在
#import "UserModel.h"
@implementation ScoreModel
+ (BOOL)propertyIsOptional:(NSString *)propertyName{
    return YES; //
}
@end
@implementation UserModel
+ (BOOL)propertyIsOptional:(NSString *)propertyName{
    return YES;
}
  • 3 修改 映射 路徑,

+(JSONKeyMapper*)keyMapper{
    // 映射路徑 : 屬性名
    return [[JSONKeyMapper alloc] initWithDictionary:@{
                                                       @"scores.english": @"eng_score"
                                                       }];
}

  • 4 舉例

    NSDictionary *dict = @{
                           @"name" : @"Jack",
                           @"address":@"浙大紫金港",
                           @"age" : @"20",
                           @"scores":@{@"english":@"99",@"chinese":@"133"},
                           @"scoreArray":@[@{@"english":@"99",@"chinese":@"133"},
                                           @{@"english":@"98",@"chinese":@"134"},
                                           @{@"english":@"97",@"chinese":@"135"}]
                           };

 
    UserModel *model = [[UserModel alloc] initWithDictionary:dict error:nil];
    NSLog(@"%@",model.name);// 正常的 字典
    NSLog(@"%@",model.scores.english);// 嵌套 model
    NSLog(@"%@",model.eng_score);// 修改映射路徑
    NSLog(@"%@",model.scoreArray[0]);// 嵌套 model數(shù)組

其他

model 屬性 不僅限于 NSString類型。
model 可以 設(shè)置 ignores

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Magical Data Modelling Framework for JSON https://github....
    橙娃閱讀 946評(píng)論 1 0
  • 前言 如今基本都是客戶端請(qǐng)求服務(wù)器的模式,數(shù)據(jù)傳遞方式基本都使用JSON的字符串方式。然而當(dāng)我們獲取了服務(wù)器發(fā)來的...
    Alibsolute閱讀 5,331評(píng)論 6 4
  • 前言 本文的demo更新在github上。 客戶端請(qǐng)求服務(wù)器,經(jīng)常使用的時(shí)JSON方式傳遞數(shù)據(jù)。一些第三方開源庫幫...
    game3108閱讀 3,328評(píng)論 3 53
  • 目錄 1 JsonModel的簡介以及XML簡介 2 JsonModel的入?yún)⑴c出參( NSString NSD...
    辛小二閱讀 2,995評(píng)論 0 6
  • 2016.09.02 By Lsz 昨天說到《肖申克的救贖》,想起與它一起出現(xiàn)在各種電影推薦排行榜上的還有《當(dāng)幸福...
    Lsuzen閱讀 288評(píng)論 0 0

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