在 Flutter 开发中,手动为复杂的 JSON 接口编写 Model 类既繁琐又容易出错(尤其是处理嵌套数组和可选类型时)。本工具能一键将 JSON 示例数据 转换为类型严谨、符合官方规范的 Dart 类定义,并自动生成序列化与反序列化逻辑。
Data Model,支持自动推导嵌套对象。Map<String, dynamic> 解析逻辑重构为强类型对象,获得编译器自动补全和类型检查。fromJson 和 toJson 模板。本站工具深度适配了 Dart 3.0+ 的语法特性与 Flutter 社区的开发习惯:
String, int, double, bool 及 Null。List<T>,对于嵌套数组也能精准推导。null 值,生成带问号的 Dart 类型(如 String? 或 int?)。factory ClassName.fromJson(Map<String, dynamic> json) 构造函数。Map<String, dynamic> toJson() 方法,方便将对象持久化。snake_case (蛇形) 命名风格转化为 Dart 建议的 camelCase (驼峰) 成员变量名。final 属性以符合不可变数据结构(Immutable)的开发模式。UserModel 或 OrderResponse)。final 属性。.dart 文件中即可使用。原始 JSON:
{
"user_name": "Gemini",
"score": 98.5,
"is_online": true,
"achievements": [101, 102]
}
生成的 Dart 代码 (本站处理):
class UserModel {
String? userName;
double? score;
bool? isOnline;
List<int>? achievements;
UserModel({this.userName, this.score, this.isOnline, this.achievements});
UserModel.fromJson(Map<String, dynamic> json) {
userName = json['user_name'];
score = json['score'];
isOnline = json['is_online'];
achievements = json['achievements']?.cast<int>();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['user_name'] = userName;
data['score'] = score;
data['is_online'] = isOnline;
data['achievements'] = achievements;
return data;
}
}