(一)ESJsonFormat是自動生成json屬性的插件,其默認源是MJExtension。若是想要和YYModel嵌套使用,須要修改下源代碼。git
默認MJExtension:github
(1)在git上下載:https://github.com/EnjoySR/ESJsonFormat-Xcodejson
(2)打開代碼源文件,在ESJsonFormatManager.m中搜索methodContentOfObjectClassInArrayWithClassInfo方法,搜索objectClassInArray替換爲modelContainerPropertyGenericClass。api
(3)command + R 運行安裝插件,徹底退出Xcode後重啓Xcode。數組
(4)loadBundle,這樣就能夠識別YYModel實現自動轉化了。安全
那麼咱們看一下ESJsonFormat效果:網絡
功能說明:框架
-0.1性能
-0.2單元測試
-0.3
-0.4
#######################################################################################################
(二)YYModel:
和MJEstension相似。可是YYModel做者測試的時候,聲明YYModel的速度和容錯率是最快的。
特性:
注意:
準備: 首先網絡中後臺接口返回的數據絕大多數都是json,咱們先準備一個json數值格式以下
{ "error": 0, "status": "success", "date": "2016-09-14", "results": [ { "currentCity": "北京", "pm25": "123", "index": [ { "title": "穿衣", "zs": "熱", "tipt": "穿衣指數", "des": "天氣熱,建議着短裙、短褲、短薄外套、T恤等夏季服裝。" }, { "title": "洗車", "zs": "較適宜", "tipt": "洗車指數", "des": "較適宜洗車,將來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。" }, { "title": "旅遊", "zs": "適宜", "tipt": "旅遊指數", "des": "天氣較好,但絲絕不會影響您的心情。微風,雖天氣稍熱,卻仍適宜旅遊,不要錯過機會呦!" }, { "title": "感冒", "zs": "少發", "tipt": "感冒指數", "des": "各項氣象條件適宜,發生感冒機率較低。但請避免長期處於空調房間中,以防感冒。" }, { "title": "運動", "zs": "較適宜", "tipt": "運動指數", "des": "天氣較好,戶外運動請注意防曬。推薦您進行室內運動。" }, { "title": "紫外線強度", "zs": "中等", "tipt": "紫外線強度指數", "des": "屬中等強度紫外線輻射天氣,外出時建議塗擦SPF高於1五、PA+的防曬護膚品,戴帽子、太陽鏡。" } ], "weather_data": [ { "date": "週三 09月14日 (實時:29℃)", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png", "weather": "多雲轉晴", "wind": "微風", "temperature": "30 ~ 20℃" }, { "date": "週四", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png", "weather": "多雲", "wind": "微風", "temperature": "30 ~ 21℃" }, { "date": "週五", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/yin.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/zhenyu.png", "weather": "陰轉陣雨", "wind": "微風", "temperature": "29 ~ 20℃" }, { "date": "週六", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/yin.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/zhenyu.png", "weather": "陰轉陣雨", "wind": "微風", "temperature": "25 ~ 19℃" } ] } ] }
下面咱們利用ESJsonFormat自動解析而且用YYModel一句話實現字典(json)轉模型。
自動轉化後的模型:
.H:
#import <Foundation/Foundation.h> @class JieGuo,Zhi,Weather_Data; @interface Weather : NSObject @property (nonatomic, copy) NSString *status; @property (nonatomic, strong) NSArray<JieGuo *> *results; @property (nonatomic, assign) NSInteger error; @property (nonatomic, copy) NSString *date; @end @interface JieGuo : NSObject @property (nonatomic, strong) NSArray<Weather_Data *> *weather_data; @property (nonatomic, copy) NSString *currentCity; @property (nonatomic, copy) NSString *pm25; @property (nonatomic, strong) NSArray<Zhi *> *index; @end @interface Zhi : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *zs; @property (nonatomic, copy) NSString *tipt; @property (nonatomic, copy) NSString *des; @end @interface Weather_Data : NSObject @property (nonatomic, copy) NSString *nightPictureUrl; @property (nonatomic, copy) NSString *weather; @property (nonatomic, copy) NSString *wind; @property (nonatomic, copy) NSString *temperature; @property (nonatomic, copy) NSString *dayPictureUrl; @property (nonatomic, copy) NSString *date; @end
.M
#import "Weather.h" @implementation Weather + (NSDictionary *)modelContainerPropertyGenericClass{ return @{@"results" : [JieGuo class]}; } @end @implementation JieGuo + (NSDictionary *)modelContainerPropertyGenericClass{ return @{@"index" : [Zhi class], @"weather_data" : [Weather_Data class]}; } @end @implementation Zhi @end @implementation Weather_Data @end
分析:已經在一個.h.m中自動完成了嵌套模型的賦值。
modelContainerPropertyGenericClass方法是YYModel的,調用自動返回另外一個嵌套模型。
主頁面代碼:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { Weather *model = [Weather yy_modelWithJSON:[self loadJson:@"Weather"]]; NSString *data = model.results[0].weather_data[0].weather; NSLog(@"%@",data); } - (NSDictionary *)loadJson:(NSString *)location { NSString *path = [[NSBundle mainBundle] pathForResource:location ofType:@"json"]; NSData *jsonData = [NSData dataWithContentsOfFile:path]; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; return jsonDict; }
打印效果:
可見,字典轉模型已經完畢了。已經拿到了所有數據的所有節點而且所有字典轉模型。下面要作的只是UI層顯示了。