ESJsonFormat-Xcode + YYModel 实现嵌套类字典转模型

 

(一)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效果:

  • 将JSON格式化输出为模型的属性
  • 生成的MJExtension框架中obectClassInArray方法(返回字典对应的模型)-->经修改后的。

  功能说明:

  

-0.1

  • 通过JSON字符串生成对应属性
  • 通过文件写入的方式生成到.m文件
  • 支持输入嵌套模型名称

-0.2

  • 支持Swift
  • 修复JSON的value的值为Null的时候多出来的空行
  • 修复BOOL类型值格式化失效问题

-0.3

  • 支持生成MJExtension框架中objectClassInArray方法
  • 修复数组嵌套多级,里面子数组不能格式化的Bug

-0.4

  • 支持格式输出到文件
  • 支持格式输出泛型(Xcode 7及之后)

  #######################################################################################################

 

(二)YYModel:

  和MJEstension类似。但是YYModel作者测试的时候,声明YYModel的速度和容错率是最快的。

  特性:

  • 高性能: 模型转换性能接近手写解析代码。
  • 自动类型转换: 对象类型可以自动转换,详情见下方表格。
  • 类型安全: 转换过程中,所有的数据类型都会被检测一遍,以保证类型安全,避免崩溃问题。
  • 无侵入性: 模型无需继承自其他基类。
  • 轻量: 该框架只有 5 个文件 (包括.h文件)。
  • 文档和单元测试: 文档覆盖率100%, 代码覆盖率99.6%。

  注意:

  准备: 首先网络中后台接口返回的数据绝大多数都是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层显示了。

 

posted @ 2016-09-14 15:41  三更小新  阅读(1835)  评论(0编辑  收藏  举报