@JsonSerializable,如何在Json中以flutter/dart打印嵌入对象



所以我试图将整个对象转换为Json。例如,如果我有两个类

@JsonSerializable
class A{
String name;
int age;
List<B> list;
A({this.name, this.age, this.list})
Map<String, dynamic> toJson() => _$AToJson(this);
factory A.fromJson(Map<String, dynamic> json) => _$AFromJson(json);
}
@JsonSerializable
Class B{
String friendsName;
int age;
B({this.friendsName, this.age})
Map<String, dynamic> toJson() => _$BToJson(this);
factory B.fromJson(Map<String, dynamic> json) => _$BFromJson(json);
}

当我尝试执行时:

print(a.toJson());// a being object of A

我得到"name"one_answers"age"很好,但"list"作为["B"的实例]

我正试图在Json中打印所有内容,如何在给定的结构中进行打印?感谢

覆盖B类的toString方法:

@JsonSerializable
class B {
String friendsName;
int age;
B({this.friendsName, this.age})
Map<String, dynamic> toJson() => _$BToJson(this);
factory B.fromJson(Map<String, dynamic> json) => _$BFromJson(json);
@override
String toString() {
return '($friendsName, $age)';
}
}