INPUT
{"title":"Post","views":12,"draft":false,"scores":[1.5],"author":{"name":"Ada"}}
OUTPUT
class Root {
final String title;
final int views;
final bool draft;
final List<double> scores;
final Author author;
Root({required this.title, required this.views, required this.draft, required this.scores, required this.author});
factory Root.fromJson(Map<String, dynamic> json) {
return Root(
title: json['title'],
views: json['views'],
draft: json['draft'],
scores: List<double>.from(json['scores']),
author: Author.fromJson(json['author']),
);
}
Map<String, dynamic> toJson() {
return {
'title': title,
'views': views,
'draft': draft,
'scores': scores,
'author': author.toJson(),
};
}
}
class Author {
final String name;
Author({required this.name});
factory Author.fromJson(Map<String, dynamic> json) {
return Author(
name: json['name'],
);
}
Map<String, dynamic> toJson() {
return {
'name': name,
};
}
}
Authorクラスは1回だけ出力され、RootのfromJson/toJsonからネスト変換として呼び出されます。