标签:
(一)ESJsonFormat是自动生成json属性的插件,其默认源是MJExtension。如果想要和YYModel嵌套使用,需要修改下源代码。
默认MJExtension:
(1)在git上下载:https://github.com/EnjoySR/ESJsonFormat-Xcode
(2)打开代码源文件,在ESJsonFormatManager.m中搜索methodContentOfObjectClassInArrayWithClassInfo方法,搜索objectClassInArray替换为modelContainerPropertyGenericClass。
(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高于15、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层显示了。
ESJsonFormat-Xcode + YYModel 实现嵌套类字典转模型
标签:
原文地址:http://www.cnblogs.com/sgxx/p/5872491.html