Skip to content

JsonKey to serialize object of object #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nisa10880 opened this issue Feb 17, 2021 · 1 comment
Closed

JsonKey to serialize object of object #802

nisa10880 opened this issue Feb 17, 2021 · 1 comment

Comments

@nisa10880
Copy link

nisa10880 commented Feb 17, 2021

Is it possible to use jsonkey to generate a line like this firstName: json ['customKey']['firstName'] as String,

Let's say I have an object like this from the server.

{
  customKey :  { firstName: ' MyFirstName'
                         lastName: 'MyLastName' 
                        },
 dateOfBirth: '1997-12-17T03:24:00' 
}

And I want to create a Flutter Class like this

@JsonSerializable(nullable: false)
class Person {
  final String firstName;
  final String lastName;
  final DateTime dateOfBirth;
  Person({this.firstName, this.lastName, this.dateOfBirth});
  factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
  Map<String, dynamic> toJson() => _$PersonToJson(this);
}
Person _$PersonFromJson(Map<String, dynamic> json) {
  return Person(
    **firstName: json['customKey']['firstName'] as String,**
    **lastName: json['customKey']['lastName'] as String,**
    dateOfBirth: DateTime.parse(json['dateOfBirth'] as String),
  );
}

Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
      'firstName': instance.firstName,
      'lastName': instance.lastName,
      'dateOfBirth': instance.dateOfBirth.toIso8601String(),
    };
@kevmoo
Copy link
Collaborator

kevmoo commented Feb 20, 2021

Duplicate of #490

@kevmoo kevmoo marked this as a duplicate of #490 Feb 20, 2021
@kevmoo kevmoo closed this as completed Feb 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants