在日常开发中 JSON 的序列化与反序列化是一个常见的操作;而 Dart 语言不支持反射,运行时反射会影响 Dart 的 tree shaking(摇树优化),tree shaking 可以“抖掉”不需要使用的代码,显著优化 App 的体积,所以 Flutter 中没有类似 Gson 这样的 Json 库,处理方法相比 Kotlin 或者 Swift 更为复杂和繁琐。
方式一: dart:convert (不推荐)
- 需要手动编码
class User {
final String name;
final String email;
User(this.name, this.email);
User.fromJson(Map<String, dynamic> json)
: name = json['name'],
email = json['email'];
Map<String, dynamic> toJson() => {
'name': name,
'email': email,
};
}
方式二:json_serializable
Flutter 官方提供了一个自动化源代码生成器 json_serializable 来生成 JSON 序列化数据模板,依然需要大量的手写代码和编写映射逻辑,比方式一友好,但依然存在多人协作难以维护的问题。
添加依赖
项目根文件夹下
运行 flutter pub add
添加依赖:
flutter pub add json_annotation dev:build_runner dev:json_serializable
运行 flutter pub get
更新本地依赖
以 json_serializable 的方式创建模型类
import 'package:json_annotation/json_annotation.dart';
/// This allows the `User` class to access private members in
/// the generated file. The value for this is *.g.dart, where
/// the star denotes the source file name.
part 'user.g.dart';
/// An annotation for the code generator to know that this class needs the
/// JSON serialization logic to be generated.
@JsonSerializable()
class User {
User(this.name, this.email);
String name;
String email;
/// A necessary factory constructor for creating a new User instance
/// from a map. Pass the map to the generated `_$UserFromJson()` constructor.
/// The constructor is named after the source class, in this case, User.
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
/// `toJson` is the convention for a class to declare support for serialization
/// to JSON. The implementation simply calls the private, generated
/// helper method `_$UserToJson`.
Map<String, dynamic> toJson() => _$UserToJson(this);
}
生成代码
在项目根目录运行命令
dart run build_runner build --delete-conflicting-outputs
或者持续监听生成
dart run build_runner watch --delete-conflicting-outputs
使用 json_serializable 模型
为了以 json_serializable
的方式解码 JSON 字符串,你不必对以前的代码做任何的改动。
final userMap = jsonDecode(jsonString) as Map<String, dynamic>;
final user = User.fromJson(userMap);
编码也是如此。调用 API 和以前一样。
String json = jsonEncode(user);
方式三:Android Studio插件 (推荐)
- 使用IDEA插件可以免去繁琐的配置和代码编写工作
- 保证生成的代码的一致性,并降低维护难度
- 适合多人协作
FlutterJsonBeanFactory
https://github.com/fluttercandies/FlutterJsonBeanFactory
安装插件
创建
在开发文件夹,右键选择JsonToDartBeanAction,如果是混合项目,注意需要单独打开 flutter 项目目录执行,否则生成会出现错误
根据页面提示填写信息
生成的代码
import 'package:wx_flutter/generated/json/base/json_field.dart';
import 'package:wx_flutter/generated/json/user_model_entity.g.dart';
import 'dart:convert';
export 'package:wx_flutter/generated/json/user_model_entity.g.dart';
@JsonSerializable()
class UserModelEntity {
late String name;
late String email;
UserModelEntity();
factory UserModelEntity.fromJson(Map<String, dynamic> json) => $UserModelEntityFromJson(json);
Map<String, dynamic> toJson() => $UserModelEntityToJson(this);
@override
String toString() {
return jsonEncode(this);
}
}
生成的 g.dart 文件
import 'package:wx_flutter/generated/json/base/json_convert_content.dart';
import 'package:wx_flutter/generated/json/base/user_model_entity.dart';
UserModelEntity $UserModelEntityFromJson(Map<String, dynamic> json) {
final UserModelEntity userModelEntity = UserModelEntity();
final String? name = jsonConvert.convert<String>(json['name']);
if (name != null) {
userModelEntity.name = name;
}
final String? email = jsonConvert.convert<String>(json['email']);
if (email != null) {
userModelEntity.email = email;
}
return userModelEntity;
}
Map<String, dynamic> $UserModelEntityToJson(UserModelEntity entity) {
final Map<String, dynamic> data = <String, dynamic>{};
data['name'] = entity.name;
data['email'] = entity.email;
return data;
}
extension UserModelEntityExtension on UserModelEntity {
UserModelEntity copyWith({
String? name,
String? email,
}) {
return UserModelEntity()
..name = name ?? this.name
..email = email ?? this.email;
}
}
自定义后缀名
字段维护
如果修改了 entity
中的字段,只需在 entity
文件中使用快捷键 alt + j
或者 option + j
,插件会自动重新生成文件
JsonToDart
https://plugins.jetbrains.com/plugin/12562-jsontodart-json-to-dart-
使用方法与上方类似