Convert JSON into POJO (Object) similar to android in Flutter
Android Studio plugin: https://plugins.jetbrains.com/plugin/12562-jsontodart-json-to-dart-
Online: https://javiercbk.github.io/json_to_dart/
Manually parse: https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51
Generate Dart classes from JSON
Convert JSON to Dart class
Generate Dart class from JSON or JSON-Schema.
json_serializable isn't that well documented, but it does exactly what you want, is easier to use and requires less boilerplate than built_value, especially when it comes to arrays.
import 'package:json_annotation/json_annotation.dart';
import 'dart:convert';
part 'school.g.dart';
@JsonSerializable()
class School {
final String name;
final int maxStudentCount;
final List<Student> students;
School(this.name, this.maxStudentCount, this.students);
factory School.fromJson(Map<String, dynamic> json) => _$SchoolFromJson(json);
Map<String, dynamic> toJson() => _$SchoolToJson(this);
}
@JsonSerializable()
class Student {
final String name;
final DateTime birthDate;
Student({this.name, this.birthDate});
factory Student.fromJson(Map<String, dynamic> json) => _$StudentFromJson(json);
Map<String, dynamic> toJson() => _$StudentToJson(this);
}
test() {
String jsonString = '''{
"name":"Trump University",
"maxStudentCount":9999,
"students":[
{
"name":"Peter Parker",
"birthDate":"1999-01-01T00:00:00.000Z"
}
]
}''';
final decodedJson = json.decode(jsonString);
final school = School.fromJson(decodedJson);
assert(school.students.length == 1);
}
It also supports enum serialization.
To generate the serialization code, run:
flutter packages pub run build_runner build