🎯 JSON 转 Dart 模型类JSON to Dart Model Class
把 JSON 转成 Flutter / Dart 模型类,自动生成构造函数、fromJson 工厂方法和 toJson 方法,支持空安全(null safety)、嵌套对象和对象数组。Turn JSON into Flutter/Dart model classes with a constructor, fromJson factory and toJson method — null-safe, with nested objects and lists of objects.
使用说明与常见问题How-to & FAQ
「JSON 转 Dart 模型类」是什么?What is the JSON to Dart Model Class tool?
把 JSON 转成 Flutter / Dart 模型类,自动生成构造函数、fromJson 工厂方法和 toJson 方法,支持空安全(null safety)、嵌套对象和对象数组。Turn JSON into Flutter/Dart model classes with a constructor, fromJson factory and toJson method — null-safe, with nested objects and lists of objects.
如何使用「JSON 转 Dart 模型类」?How do I use the JSON to Dart Model Class?
粘贴 JSON 并填写根类名,下方生成完整的 Dart 类。用法:final user = Root.fromJson(jsonDecode(text)); 反向用 jsonEncode(user.toJson())。Paste JSON and set the root class name to get complete Dart classes. Use them with final user = Root.fromJson(jsonDecode(text)); and jsonEncode(user.toJson()).
数字为什么用 (json[...] as num).toDouble()?Why (json[...] as num).toDouble() for numbers?
JSON 里的 12 和 12.0 在 Dart 解码后可能是 int 也可能是 double,直接 as double 会在遇到整数时报错。先转成 num 再 toDouble()/toInt() 更稳妥。In Dart, a decoded JSON number may be int or double, so a direct "as double" throws on whole numbers. Casting to num first and then calling toDouble()/toInt() is safe.
生成的代码需要 json_serializable 吗?Do I need json_serializable?
不需要。生成的是手写风格的 fromJson / toJson,只依赖 dart:convert,复制到项目里就能用。No. The output is hand-written style fromJson/toJson that only needs dart:convert — paste it into your project and use it.
「JSON 转 Dart 模型类」免费吗?数据会上传吗?Is the JSON to Dart Model Class free? Is my data uploaded?
「JSON 转 Dart 模型类」完全免费,无需注册或安装,手机和电脑都能用;所有处理都在你的浏览器本地完成,输入的内容和文件不会上传到服务器。The JSON to Dart Model Class is completely free with no sign-up or install and works on phones and desktops; everything runs locally in your browser and nothing you enter is uploaded.