在mvvm 开发模式下使用JSonModel解析数据

/**

         *  JsonModel

         */

 主要是讲了 JsonModel解析数据时遇到点三种不同的解法

//      系统返回的Json

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[Json dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error:&error];

        _user = [[User alloc] initWithDictionary:dic error:nil];

        1.Json只是单纯的字典时 直接解析就行

        User.h

        @interface User : JSONModel

        @property (nonatomic, copy) NSString <Optional>* password;//

        @property (nonatomic, copy) NSString <Optional>* phone;//

        @property (nonatomic, copy) NSString <Optional>* infor;//用户id

        @end

        User.m

        @implementation User

        

        @end

        

        2.当字典里还有json 数据时 要在User 里面进行第二次解析

        例如上面的infor的值JSon 类型 哪么应该这样

        User.h

        @interface User : JSONModel

        @property (nonatomic, copy) NSString <Optional>* password;//

        @property (nonatomic, copy) NSString <Optional>* phone;//

        @property (nonatomic, copy) NSString <Optional>* infor;//

        @property (nonatomic, copy) Infor <Optional>* objInfor;//

        @end

//        在对infor 第二次解析 解析成类Infor

        @interface Infor : JSONModel

        @property (nonatomic, copy) NSString <Optional>* phone;

        @property (nonatomic, copy) NSString <Optional>* address;

        @end

        

        User.m

        @implementation User

        - (void)setInfor:(NSString *)infor{

            _infor = infor;

            

            self.objInfor = [[Infor alloc]initWithData:[_infor dataUsingEncoding:NSUTF8StringEncoding] error:nil];

        }

        @end

        

        @implementation Infor

        

        @end

        

#pragma mark -- 如果返回的是数组的话

        /**

         *  数组为GoodsTimeList 数组里面的每一组数据是GoodsTime

         */

        User.h

        @interface GoodsTime : JSONModel

        @property (nonatomic, copy) NSString <Optional>* goods_time_id;

        @property (nonatomic, copy) NSString <Optional>* goods_time_name;

        @end

        

        @protocol GoodsTime @end

        @interface GoodsTimeList : JSONModel

        @property (strong,nonatomic) NSArray<GoodsTime,Optional> *GoodsTimeList;

        @end

        

        User.m

        @implementation GoodsTime

        

        @end

        

        @implementation GoodsTimeList

        

        @end

        

#pragma mark --如果从服务器收到的数据不符合规则的话 需要自己拼凑成方便解析的类型

        /**

         *  配合上面的例子来说 如果服务端返回的数组前面没有数组名的话 可以自己添加一个数组名字如下

         */

        NSString *str = [NSString stringWithFormat:@"{\"GoodsTimeList\":%@}",Json];

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];

        self.goodsTimeList = [[GoodsTimeList alloc] initWithDictionary:dic error:&error];

        

        

        以上就是利用JsonModel 解析数据的一些方法了 个人极少写文章 表达不清希望见谅 !一般用在mvvm

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值