'Nested Json Serialise with Custom Class
I have a JSON data from the server from which I want to add specific JSON data to another class but maintaining its instance in the parent class
Json data from server: { "id:"1, "name":"mike", "classname:"STD-4" }
@JsonSerialize
class Student{
@JsonKey(name:"id")
int id;
StudentInfo studentInfo;
}
@JsonSerialize
class StudentInfo{
@JsonKey(name:"name")
String name;
@JsonKey(name:"classname")
String classname;
}
Is this possible with JsonSerialize lib in flutter?
Solution 1:[1]
Yes, it should work fine. I assume you are using this package - https://pub.dev/packages/json_serializable?
There is also a parameter called explicitToJson, read more about it at https://pub.dev/documentation/json_serializable/latest/type_helper/ClassConfig/explicitToJson.html.
Solution 2:[2]
@JsonSerializable(explicitToJson: true)
class Student{
@JsonKey(name:"id")
int id;
StudentInfo studentInfo;
}
@JsonSerializable(explicitToJson: true)
class StudentInfo{
@JsonKey(name:"name")
String name;
@JsonKey(name:"classname")
String classname;
}
@JsonSerializable(explicitToJson: true)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yash Garg |
| Solution 2 | Hyungju Moon |
