Skip to content

JsonKey to serialize object of object #802

Closed
@nisa10880

Description

@nisa10880

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(),
    };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions