jsont to dart code example

Example 1: json to dart

class Autogenerated {
  int postId;
  int id;
  String name;
  String email;
  String body;

  Autogenerated({this.postId, this.id, this.name, this.email, this.body});

  Autogenerated.fromJson(Map<String, dynamic> json) {
    postId = json['postId'];
    id = json['id'];
    name = json['name'];
    email = json['email'];
    body = json['body'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['postId'] = this.postId;
    data['id'] = this.id;
    data['name'] = this.name;
    data['email'] = this.email;
    data['body'] = this.body;
    return data;
  }
}

Example 2: javiercbk

class Message {
  String message;
  String fullname;
  String image;
  int messageCount;

  Message({this.message, this.fullname, this.image, this.messageCount});

  Message.fromJson(Map<String, dynamic> json) {
    message = json['message'];
    fullname = json['fullname'];
    image = json['image'];
    messageCount = json['messageCount'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['message'] = this.message;
    data['fullname'] = this.fullname;
    data['image'] = this.image;
    data['messageCount'] = this.messageCount;
    return data;
  }
}