Flutter中JSON解析的等效实现是什么?是否有GSON/Jackson/Moshi同类工具?
Great question! In Flutter (which runs on Dart under the hood), you’ve got plenty of solid alternatives to Java’s GSON, Jackson, or Moshi for JSON parsing. Let’s break down the main options, how they work, and which ones align with the functionality you’re used to.
1. Native Dart Solution: dart:convert
Dart comes with a built-in dart:convert library—think of this as the "vanilla" option, no external dependencies required. It’s perfect for simple use cases where you don’t want to add extra packages, similar to writing manual deserializers in GSON for small models.
Example Usage:
Suppose you have a basic User class:
class User { final String name; final int age; User({required this.name, required this.age}); // Factory constructor to map JSON to User factory User.fromJson(Map<String, dynamic> json) { return User( name: json['name'], age: json['age'], ); } // Convert User back to JSON Map<String, dynamic> toJson() { return { 'name': name, 'age': age, }; } }
Parsing and serializing JSON looks like this:
import 'dart:convert'; void main() { String jsonString = '{"name": "Alice", "age": 30}'; Map<String, dynamic> jsonMap = jsonDecode(jsonString); User user = User.fromJson(jsonMap); print(user.name); // Output: Alice // Convert user back to JSON string String newJson = jsonEncode(user.toJson()); }
It’s straightforward, but gets tedious for complex models with nested objects—you’ll have to write all fromJson/toJson methods manually.
2. Code Generation Libraries (Auto-Magic Boilerplate)
For larger apps with complex models, you’ll want tools that auto-generate serialization/deserialization code—just like how GSON/Jackson/Moshi handle things with annotations. These are the most popular options in the Flutter ecosystem:
json_serializable
This is the go-to package for most Flutter developers. It uses annotations to generate all the boilerplate fromJson/toJson code, just like Moshi’s Kotlin annotation processing.
Setup & Example:
- Add dependencies to your
pubspec.yaml:
dependencies: json_annotation: ^4.8.1 dev_dependencies: build_runner: ^2.4.4 json_serializable: ^6.7.1
- Annotate your model class:
import 'package:json_annotation/json_annotation.dart'; // This part file will be auto-generated part 'user.g.dart'; @JsonSerializable() class User { final String name; final int age; User({required this.name, required this.age}); // Auto-generated deserialization method factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json); // Auto-generated serialization method Map<String, dynamic> toJson() => _$UserToJson(this); }
- Run the code generator:
flutter pub run build_runner build
This creates the user.g.dart file with all the serialization logic, handling nested objects, optional fields, custom converters, and more.
freezed
If you want immutable classes, copyWith methods, and union types alongside JSON parsing, freezed is a fantastic choice. It pairs seamlessly with json_serializable to give you all those features plus automatic JSON handling—like combining Jackson’s data binding with immutable value classes.
Example:
import 'package:freezed_annotation/freezed_annotation.dart'; part 'user.freezed.dart'; part 'user.g.dart'; @freezed class User with _$User { const factory User({ required String name, required int age, }) = _User; // Auto-generated JSON deserialization factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json); }
Run the build command, and you’ll get immutable models, easy object copying, and JSON parsing all in one.
built_value
For strict type safety, validation, and advanced features like hash code/equality checks, built_value is a powerful option. It enforces immutability and generates robust serialization code, making it ideal for large-scale apps where type safety is non-negotiable—similar to Jackson’s strict mode.
3. Runtime Serialization Libraries (No Code Generation)
If you prefer a "no boilerplate, no build steps" approach (like GSON working without annotations for simple cases), there are runtime reflection-based options:
dart_json_mapper
This library uses reflection to serialize/deserialize objects at runtime. Just annotate your classes with @jsonSerializable and you’re good to go.
Example:
import 'package:dart_json_mapper/dart_json_mapper.dart'; @jsonSerializable class User { String name; int age; User(this.name, this.age); } void main() { final user = User('Bob', 25); String json = JsonMapper.serialize(user); User deserializedUser = JsonMapper.deserialize<User>(json)!; }
Note: Reflection can have performance implications for large apps, so code generation is still recommended for most production use cases.
Quick Summary
- Simple cases: Use
dart:convert(native, no dependencies) - Most apps:
json_serializable(balance of ease and flexibility) - Immutable models + JSON:
freezed - Strict type safety:
built_value - No code generation:
dart_json_mapper
内容的提问来源于stack exchange,提问作者Mandeep Yadav

