【Flutter】JSON 模型转换 ( JSON 序列化工具 | JSON 手动序列化 | 根据 JSON 编写 Dart 模型类 | 在线自动根据 JSON 转换 Dart 类 )
文章目录
- 一、JSON 序列化工具
- 二、JSON 手动序列化
- 三、根据 JSON 编写 Dart 模型类
- 四、在线自动转换
- 五、相关资源
一、JSON 序列化工具
JSON 格式比较简单的话 , 使用自带的 dart:convert 包 , 手动进行 JSON 的序列化与反序列化的操作即可 ;
/// json 序列化 , 反序列化 包
import 'dart:convert';
如果 JSON 格式很复杂 , 就需要使用 JSON 的序列化插件 ;
- json_serializable : https://pub.dev/packages/json_serializable
- built_value : https://pub.dev/packages/built_value
二、JSON 手动序列化
给定如下 JSON 字符串 :
{"icon": "icon.png","title": "标题","url": "https://www.baidu.com/","statusBarColor": "FFFFFF","hideAppBar": true
}
写成一行 :
{ "icon": "icon.png", "title": "标题", "url": "https://www.baidu.com/", "statusBarColor": "FFFFFF", "hideAppBar": true }
将上述 JSON 字符串序列化为 Map<String, dynamic> 格式的数据 ;
代码示例 :
import 'dart:convert';void main() {String jsonString = '{ "icon": "icon.png", "title": "标题", "url": "https://www.baidu.com/", "statusBarColor": "FFFFFF", "hideAppBar": true }';/// 处理中文乱码Utf8Codec utf8codec = Utf8Codec();Utf8Decoder utf8decoder = Utf8Decoder();Utf8Encoder utf8encoder = Utf8Encoder();/// 将二进制 Byte 数据以 UTF-8 格式编码 , 获取编码后的字符串String responseString = utf8decoder.convert(utf8codec.encode(jsonString));// 将 json 字符串信息转为 Map<String, dynamic> 类型的键值对信息Map<String, dynamic> jsonMap = json.decode(responseString);// 使用工厂方法构造 Dart 对象CommonModel commonModel = CommonModel.fromJson(jsonMap);print('icon : ${commonModel.icon}\ntittle : ${commonModel.title}\nurl : ${commonModel.url}');
}// Dart 模型类
class CommonModel {final String? icon;final String? title;final String? url;final String? statusBarColor;final bool? hideAppBar;CommonModel({this.icon, this.title, this.url, this.statusBarColor, this.hideAppBar});factory CommonModel.fromJson(Map<String, dynamic> json) {return CommonModel(icon: json['icon'],title: json['title'],url: json['url'],statusBarColor: json['statusBarColor'],hideAppBar: json['hideAppBar'],);}
}
执行结果 :
icon : icon.png
tittle : 标题
url : https://www.baidu.com/
三、根据 JSON 编写 Dart 模型类
给定一个指定格式的 JSON 类 , 将其转为 Dart , 如果进行手动转换 ,
{"school": "第一小学","students": [{"name": "小王","age": "12"},{"name": "小白","age": "13"}]
}
成员变量是普通变量的情况 : 没有使用 final 修饰 ;
class School {/// json 字符串中 school 字段String? school;/// json 字符串中的 students 数组List<Student>? students;School({this.school, this.students});/// 构造方法有两种写法/// 参数不是 final 类型的 , 就使用这种方式编写/// 方法前不需要添加 factory/// 如果成员是 final 类型的 , 那么方法前需要加入 factorySchool.fromJson(Map<String, dynamic> json) {school = json['school'];/// 先将 json 数组转为 List/// 然后调用 map 方法 , 为具体的每个元素赋值(json['students'] as List).map((i) => Student.fromJson(i));}
}class Student {String? name;String? age;Student({this.name, this.age});Student.fromJson(Map<String, dynamic> json) {name = json['name'];age = json['age'];}
}
成员变量使用 final 修饰的情况 :
class School {/// json 字符串中 school 字段final String? school;/// json 字符串中的 students 数组final List<Student>? students;School({this.school, this.students});/// 构造方法有两种写法/// 参数不是 final 类型的 , 就使用这种方式编写/// 方法前不需要添加 factory/// 如果成员是 final 类型的 , 那么方法前需要加入 factoryfactory School.fromJson(Map<String, dynamic> json) {String school = json['school'];/// 先将 json 数组转为 List/// 然后调用 map 方法 获取每个值List<Student> students = (json['students'] as List).map((i) => Student.fromJson(i)).toList();return School(school: school, students: students);}
}class Student {final String? name;final String? age;Student({this.name, this.age});factory Student.fromJson(Map<String, dynamic> json) {String name = json['name'];String age = json['age'];return Student(name: name, age: age);}
}
四、在线自动转换
除了转为 Dart 类型之外 , 其它 语言 类型 也可以转换 , https://www.bejson.com/json2javapojo/new/ 网站可以 JSON 转 JavaBean ;
推荐一个 JSON 转 Dart 的工具网站 : https://jsontodart.com/
这是系统根据 JSON 字符串自动生成的 Dart 类 ;
class Autogenerated {String school;List<Students> students;Autogenerated({this.school, this.students});Autogenerated.fromJson(Map<String, dynamic> json) {school = json['school'];if (json['students'] != null) {students = new List<Students>();json['students'].forEach((v) {students.add(new Students.fromJson(v));});}}Map<String, dynamic> toJson() {final Map<String, dynamic> data = new Map<String, dynamic>();data['school'] = this.school;if (this.students != null) {data['students'] = this.students.map((v) => v.toJson()).toList();}return data;}
}class Students {String name;String age;Students({this.name, this.age});Students.fromJson(Map<String, dynamic> json) {name = json['name'];age = json['age'];}Map<String, dynamic> toJson() {final Map<String, dynamic> data = new Map<String, dynamic>();data['name'] = this.name;data['age'] = this.age;return data;}
}
五、相关资源
参考资料 :
- Flutter 官网 : https://flutter.dev/
- Flutter 插件下载地址 : https://pub.dev/packages
- Flutter 开发文档 : https://flutter.cn/docs ( 强烈推荐 )
- 官方 GitHub 地址 : https://github.com/flutter
- Flutter 中文社区 : https://flutter.cn/
- Flutter 实用教程 : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart 中文文档 : https://dart.cn/
- Dart 开发者官网 : https://api.dart.dev/
- Flutter 中文网 : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter 相关问题 : https://flutterchina.club/faq/ ( 入门阶段推荐看一遍 )
- GitHub 上的 Flutter 开源示例 : https://download.csdn.net/download/han1202012/15989510
- Flutter 实战电子书 : https://book.flutterchina.club/chapter1/
- Dart 语言练习网站 : https://dartpad.dartlang.org/
重要的专题 :
- Flutter 动画参考文档 : https://flutterchina.club/animations/
博客源码下载 :
GitHub 地址 : https://github.com/han1202012/flutter_http( 随博客进度一直更新 , 有可能没有本博客的源码 )
博客源码快照 : ( 本篇博客的源码快照 , 可以找到本博客的源码 )
【Flutter】JSON 模型转换 ( JSON 序列化工具 | JSON 手动序列化 | 根据 JSON 编写 Dart 模型类 | 在线自动根据 JSON 转换 Dart 类 )相关推荐
- 【序列化】Java中将使用PHP序列化工具将数据序列化
在项目中需要和PHP公用一个MySQL数据库,有些数据需要序列化之后保存,这就需要将待存储的数据序列化之后存到数据库中,取出的时候,需要反序列化之后才能正常使用. 原数据: {"060088 ...
- 面向航空航天工业领域的基于模型的仿真验证工具SkyEye
我们一直致力于提供给航空航天制造商一套全数字的优化方案--面向航空航天工业领域的基于模型的仿真验证工具SkyEye.我们的仿真验证技术可用于开发高复杂度和高保真度的模型,对发动机,飞机的飞控进行预测性 ...
- ppt转换成pdf工具
ppt转换成pdf工具 前言:朋友在微博给我留言,让我帮忙把ppt文件转换成pdf,当时也是信口开河,认为直接下载几个转换软件就能马上搞定,但是,一测试却发现,转换之后的文本不是出现了乱码就是转换不出 ...
- 第10章第17节:使用iSlide的序列化工具设置动画的延迟时间 [PowerPoint精美幻灯片实战教程]
您将在本节通过序列化工具,设置动画的延迟时间.首先点击此处的扩展工具,显示扩展工具列表. 选择序列化工具,打开序列化设置窗口. 当前幻灯片中的所有对象都拥有动画效果,点击此处的复选框,全选需要设置序列 ...
- 解决Python自带的json序列化工具不能序列化datetime类型数据问题
解决Python自带的json序列化工具不能序列化datetime类型数据问题 参考文章: (1)解决Python自带的json序列化工具不能序列化datetime类型数据问题 (2)https:// ...
- 迄今为止最快的 JSON 序列化工具 Jil
2019独角兽企业重金招聘Python工程师标准>>> 迄今为止最快的 JSON 序列化工具 Jil https://github.com/kevin-montrose/Jil 转载 ...
- .NET序列化工具Jil、Json.NET和Protobuf的简单测评
前一段时间逛园子的时候发现有人比较了Jil.Json.NET和Protobuf的性能,一时好奇,也做了个测试,这里记录下来,以供查阅. 前期准备 依赖类库的话,可以通过Nuget在公共组件库总下载,这 ...
- java rest 序列化_http请求/restful/序列化反序列化/JSON
1.为什么要实现Serializable (1)将内存中的对象数据存在磁盘中持久化 (2)将对象从一个应用程序发送到另一个应用程序 实现Serializable接口可以把对象序列化为字节流,实现传输必 ...
- Json完整版介绍和工具类解析应用
JAVA序列化与JSON数据之间的关系 1.一种轻量级数据交换的文本格式,而不是一种编程语言. 2.采用完全独立于编程语言的文本格式来存储和表示(展示)数据. 3.独立于语言和平台 4.JSON 解析 ...
最新文章
- 朴素贝叶斯算法python sklearn实现_朴素贝叶斯算法优化与 sklearn 实现
- lazada开店平台费用都有哪些,产品如何来做定价?
- 秒后面的单位是什么_比光年还大的长度单位,你知道几个?
- mysql设置token有效期_记住我 token保存到数据库
- 【渝粤教育】广东开放大学 劳动关系理论与实务 形成性考核 (1)
- Jetty 和tomcat 比较研究初探
- apache commons - lang 常用方法记录
- 【java】创建一个Frame,字体的颜色可以用按钮改变
- python写数据库中间件_python 20 day form、model多对多操作、sql、中间件
- Android P新特性
- 复现贪吃蛇程序——判断游戏失败(第四部分)
- AntDesign 实战教程 --------前言
- 冒泡法java程序图片_正宗冒泡法-java语言实现
- python具体学什么-python主要用于什么开发
- 面试网答题(如老牛网)
- 使用Phaser和HTML5特性检测移动设备旋转重力方向
- 2022世界燕窝展|上海滋补品展|虫草、海参展谈卖货,还是卖品牌?不再简单。
- 郭炜实用Python程序设计慕课:chapter13面向对象程序设计
- 由计算机病毒引起的现象,计算机病毒的症状
- 如何开始编写微信小程序