json_model
json_model copied to clipboard
How to deal with JSON index keys?
Hi there,
So, I have a JSON like this
{
"groups": {
"1": {
"members": {
"1": {
"id": "1",
"groupId": "1"
}
}
},
"2": {
"members": {
"1": {
"id": "1",
"groupId": "1"
}
}
},
"3": {
"members": {
"1": {
"id": "1",
"groupId": "1"
}
}
},
"4": {
"members": {
"1": {
"id": "1",
"groupId": "1"
}
}
}
}
}
It is not exactly "JSON index keys", but it is a dynamic naming number. Sometimes it will have 1, sometimes it will have 100++. How do I config this?
It would be something like this?
{
"@JsonKey(ignore: false) unknownEnumValue": "$group_obj"
}
From another tool I can get:
class State {
String counter;
Map<String, Group> groups;
State({
this.counter,
this.groups
});
factory State.fromJson(Map<String, dynamic> json) => State(
counter: json["counter"],
groups: Map.from(json["groups"]).map((k, v) => MapEntry<String, Group>(k, Group.fromJson(v))),
);
Map<String, dynamic> toJson() => {
"counter": counter,
"groups": Map.from(groups).map((k, v) => MapEntry<String, dynamic>(k, v.toJson())),
};
}
class Group {
Members members;
Group({
this.members
});
factory Group.fromJson(Map<String, dynamic> json) => Group(
members: Members.fromJson(json["members"]),
);
Map<String, dynamic> toJson() => {
"members": members.toJson(),
};
}
//so on