• flutter 书写json解析类


    首先要明确json数据的类型
    Map(键-值对)即Map

    {
      "id":"487349",
      "name":"Pooja Bhaumik",
      "score" : 1000
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    fromJson() 方法将JSON字符串解析为java对象,Gson的toJson() 方法将Java对象转换为JSON string

    总共有三种类型,第一种是单纯的基本类型int,String,第二种是对象,范类DriftBottleInfo? driftBottleInfo; 第三种是列表List? downList;

    最基本的一种类型写法,类里面包含fromJson和toJson两种方法

    class DownDriftBottle {
      int? downBoxId;
    
      DownDriftBottle.fromJson(dynamic json) {//fromJson解析服务器返回的json数据,
        downBoxId = json['down_box_id'];//然后赋值,可以把json['down_box_id']直接看作是服务器返回的那个值了
      }
    
    //自己的数据转成json数据,Json数据是 "id":"487349",前面肯定是String类型,后面是dynamic类型,所以toJson方法是 Map类型
      Map<String, dynamic> toJson() {
        final map = <String, dynamic>{};//创建一个map
        map['down_box_id'] = downBoxId;//赋值
        return map;//返回
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    基本类型加对象 
    class UpDriftBottle {
      int? upBoxId; //捞到瓶子到ID
      BottleUserInfo? userInfo; //用户信息
    
      UpDriftBottle.fromJson(dynamic json) {
        upBoxId = json['up_box_id'];
    
        if (json['user_info'] != null) {//先判断不为null
          userInfo= BottleUserInfo.fromJson(json['user_info']);//不是UpDriftBottle这个类解析了,是用BottleUserInfo这个类进行解析了
        }
      }
    
    //没用到的话可以不用写的
      Map<String, dynamic> toJson() {
        final map = <String, dynamic>{};
        map['up_box_id'] = upBoxId;
        return map;
      }
    }
    
    class BottleUserInfo {
      String? avatar;
      String? avatarThumb;
      int? birthday;
      String? nickname;
      int? sex;
      String? sign;
      int? userId;
      int? vipExpirationTime;
      int? vipLevel;
      num? dist;
    
      BottleUserInfo.fromJson(dynamic json) {
        avatar = json['avatar'];
        avatarThumb = json['avatar_thumb'];
        birthday = json['birthday'];
        dist = json['dist'];
        nickname = json['nickname'];
        sex = json['sex'];
        sign = json['sign'];
        userId = json['user_id'];
        vipExpirationTime = json['vip_expiration_time'];
        vipLevel = json['vip_level'];
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    对象和列表

    class DriftBottleSetting {
      RemainTimes? remainTimes;
      List<bottleSettingBean>? bottleSetting;//列表是范类
    
      DriftBottleSetting.fromJson(dynamic json) {
    
        if (json['remain_times'] != null) {//对象判空
          remainTimes= RemainTimes.fromJson(json['remain_times']);//用RemainTimes来解析
        }
    
        if (json['bottle_setting'] != null) {//判空
          bottleSetting = [];//创建一个列表
          json['bottle_setting'].forEach((v) {//for循环,解析json里面的数据,然后将值全班装进列表里面
            bottleSetting!.add(bottleSettingBean.fromJson(v));
          });
        }
      }
    }
    
    class RemainTimes {
      int? remainFishTimes;
      int? remainThrowTimes;
      int? bottleNum;
    
      RemainTimes.fromJson(dynamic json) {
        remainFishTimes = json['remain_fish_times'];
        remainThrowTimes = json['remain_throw_times'];
        bottleNum = json['bottle_num'];
      }
    }
    
    class bottleSettingBean {
      int? id;
      int? type;
      int? price;
      String? desc;
      bottleSettingBean.fromJson(dynamic json) {
        id = json['id'];
        type = json['type'];
        price = json['price'];
        desc = json['desc'];
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    tojson的三种类型,基本类型,对象和列表,总的解析类在下面

    /// down_list : [{"app_id":"string","create_time":0,"drift_bottle_id":0,"text":"string","user_id":0}]
    /// my_info : {"avatar":"string","avatar_thumb":"string","birthday":0,"nickname":"string","sex":0}
    /// other_info : {"avatar":"string","avatar_thumb":"string","birthday":0,"nickname":"string","sex":0}
    
    class ReplyList {
      ReplyList({
        List<DownList>? downList,
        MyInfo? myInfo,
        OtherInfo? otherInfo,
      }) {
        _downList = downList;
        _myInfo = myInfo;
        _otherInfo = otherInfo;
      }
    
      ReplyList.fromJson(dynamic json) {
        if (json['down_list'] != null) {
          _downList = [];
          json['down_list'].forEach((v) {
            _downList?.add(DownList.fromJson(v));
          });
        }
        _myInfo = json['my_info'] != null ? MyInfo.fromJson(json['my_info']) : null;
        _otherInfo = json['other_info'] != null
            ? OtherInfo.fromJson(json['other_info'])
            : null;
      }
    
      List<DownList>? _downList;
      MyInfo? _myInfo;
      OtherInfo? _otherInfo;
    
      ReplyList copyWith({
        List<DownList>? downList,
        MyInfo? myInfo,
        OtherInfo? otherInfo,
      }) =>
          ReplyList(
            downList: downList ?? _downList,
            myInfo: myInfo ?? _myInfo,
            otherInfo: otherInfo ?? _otherInfo,
          );
    
      List<DownList>? get downList => _downList;
    
      MyInfo? get myInfo => _myInfo;
    
      OtherInfo? get otherInfo => _otherInfo;
    
      Map<String, dynamic> toJson() {
        final map = <String, dynamic>{};
        if (_downList != null) {
          map['down_list'] = _downList?.map((v) => v.toJson()).toList();
        }
        if (_myInfo != null) {
          map['my_info'] = _myInfo?.toJson();
        }
        if (_otherInfo != null) {
          map['other_info'] = _otherInfo?.toJson();
        }
        return map;
      }
    }
    
    /// avatar : "string"
    /// avatar_thumb : "string"
    /// birthday : 0
    /// nickname : "string"
    /// sex : 0
    
    class OtherInfo {
      OtherInfo({
        String? avatar,
        String? avatarThumb,
        int? birthday,
        String? nickname,
        int? sex,
      }) {
        _avatar = avatar;
        _avatarThumb = avatarThumb;
        _birthday = birthday;
        _nickname = nickname;
        _sex = sex;
      }
    
      OtherInfo.fromJson(dynamic json) {
        _avatar = json['avatar'];
        _avatarThumb = json['avatar_thumb'];
        _birthday = json['birthday'];
        _nickname = json['nickname'];
        _sex = json['sex'];
      }
    
      String? _avatar;
      String? _avatarThumb;
      int? _birthday;
      String? _nickname;
      int? _sex;
    
      OtherInfo copyWith({
        String? avatar,
        String? avatarThumb,
        int? birthday,
        String? nickname,
        int? sex,
      }) =>
          OtherInfo(
            avatar: avatar ?? _avatar,
            avatarThumb: avatarThumb ?? _avatarThumb,
            birthday: birthday ?? _birthday,
            nickname: nickname ?? _nickname,
            sex: sex ?? _sex,
          );
    
      String? get avatar => _avatar;
    
      String? get avatarThumb => _avatarThumb;
    
      int? get birthday => _birthday;
    
      String? get nickname => _nickname;
    
      int? get sex => _sex;
    
      Map<String, dynamic> toJson() {
        final map = <String, dynamic>{};
        map['avatar'] = _avatar;
        map['avatar_thumb'] = _avatarThumb;
        map['birthday'] = _birthday;
        map['nickname'] = _nickname;
        map['sex'] = _sex;
        return map;
      }
    }
    
    /// avatar : "string"
    /// avatar_thumb : "string"
    /// birthday : 0
    /// nickname : "string"
    /// sex : 0
    
    class MyInfo {
      MyInfo({
        String? avatar,
        String? avatarThumb,
        int? birthday,
        String? nickname,
        int? sex,
      }) {
        _avatar = avatar;
        _avatarThumb = avatarThumb;
        _birthday = birthday;
        _nickname = nickname;
        _sex = sex;
      }
    
      MyInfo.fromJson(dynamic json) {
        _avatar = json['avatar'];
        _avatarThumb = json['avatar_thumb'];
        _birthday = json['birthday'];
        _nickname = json['nickname'];
        _sex = json['sex'];
      }
    
      String? _avatar;
      String? _avatarThumb;
      int? _birthday;
      String? _nickname;
      int? _sex;
    
      MyInfo copyWith({
        String? avatar,
        String? avatarThumb,
        int? birthday,
        String? nickname,
        int? sex,
      }) =>
          MyInfo(
            avatar: avatar ?? _avatar,
            avatarThumb: avatarThumb ?? _avatarThumb,
            birthday: birthday ?? _birthday,
            nickname: nickname ?? _nickname,
            sex: sex ?? _sex,
          );
    
      String? get avatar => _avatar;
    
      String? get avatarThumb => _avatarThumb;
    
      int? get birthday => _birthday;
    
      String? get nickname => _nickname;
    
      int? get sex => _sex;
    
      Map<String, dynamic> toJson() {
        final map = <String, dynamic>{};
        map['avatar'] = _avatar;
        map['avatar_thumb'] = _avatarThumb;
        map['birthday'] = _birthday;
        map['nickname'] = _nickname;
        map['sex'] = _sex;
        return map;
      }
    }
    
    /// app_id : "string"
    /// create_time : 0
    /// drift_bottle_id : 0
    /// text : "string"
    /// user_id : 0
    
    class DownList {
      DownList({
        String? appId,
        int?    createTime,
        int?    driftBottleId,
        String? text,
        int?    userId,
      }) {
        _appId = appId;
        _createTime = createTime;
        _driftBottleId = driftBottleId;
        _text = text;
        _userId = userId;
      }
    
      DownList.fromJson(dynamic json) {
        _appId = json['app_id'];
        _createTime = json['create_time'];
        _driftBottleId = json['drift_bottle_id'];
        _text = json['text'];
        _userId = json['user_id'];
      }
    
      String? _appId;
      int? _createTime;
      int? _driftBottleId;
      String? _text;
      int? _userId;
    
      DownList copyWith({
        String? appId,
        int? createTime,
        int? driftBottleId,
        String? text,
        int? userId,
      }) =>
          DownList(
            appId: appId ?? _appId,
            createTime: createTime ?? _createTime,
            driftBottleId: driftBottleId ?? _driftBottleId,
            text: text ?? _text,
            userId: userId ?? _userId,
          );
    
      String? get appId => _appId;
    
      int? get createTime => _createTime;
    
      int? get driftBottleId => _driftBottleId;
    
      String? get text => _text;
    
      int? get userId => _userId;
    
      Map<String, dynamic> toJson() {
        final map = <String, dynamic>{};
        map['app_id'] = _appId;
        map['create_time'] = _createTime;
        map['drift_bottle_id'] = _driftBottleId;
        map['text'] = _text;
        map['user_id'] = _userId;
        return map;
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277

    基本类型直接赋值

      Map<String, dynamic> toJson() {
        final map = <String, dynamic>{};
        map['avatar'] = _avatar;
        map['avatar_thumb'] = _avatarThumb;
        map['birthday'] = _birthday;
        map['nickname'] = _nickname;
        map['sex'] = _sex;
        return map;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    对象要先toJson再赋值,对象的那个类里面要写toJson方法,tojson基本类型
    列表的要将每一项都进行tojson,然后组成list,最后再赋值

      Map<String, dynamic> toJson() {
        final map = <String, dynamic>{};
        if (_downList != null) {
          map['down_list'] = _downList?.map((v) => v.toJson()).toList();//列表
        }
        if (_myInfo != null) {//对象
          map['my_info'] = _myInfo?.toJson();
        }
        if (_otherInfo != null) {//对象
          map['other_info'] = _otherInfo?.toJson();
        }
        return map;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    自适应AI chatGPT智能聊天创作官网html源码/最新AI创作系统/ChatGPT商业版网站源码
    take和 drop功能还有takewhile 和 dropwhile 功能主要用于分开list
    深入理解强化学习——马尔可夫决策过程:马尔可夫决策过程和马尔可夫过程/马尔可夫奖励过程的区别
    Python(三)数据类型转换
    EOS区块链keosd的RPC API
    9.23/24数电
    群面经验笔记本(持续更新ing...)
    java反序列化专项
    Linux磁盘扩容(加硬盘法)
    性能测试-性能工程落地的4个阶段(21)
  • 原文地址:https://blog.csdn.net/weixin_44911775/article/details/126223179