flutter

Converting class objects to JSON string in Flutter

class Employee {
  final String name;
  final String email;

  Employee(this.name, this.email);

  Employee.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        email = json['email'];

  Map<String, dynamic> toJson() =>
    {
      'name': name,
      'email': email,
    };
}

//Now CONVERT SIMPLE JSON TO FLUTTER OBJECT 
Map employeeMap = jsonDecode(jsonString);
var employee = Employee.fromJson(employeeMap);

//CONVERT FLUTTER OBJECT TO SIMPLE JSON STRING
String json = jsonEncode(employee);

Remember that there must be 'Map<String, dynamic> toJson()' function if you want to convert flutter class object to JSON string. You can return an object from it like simple JSON objects. 

class EventResponse {
  bool? status;
  List<EventDetails>? data;
  String? message;

  EventResponse({this.status, this.data, this.message});

  EventResponse.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    if (json['data'] != null) {
      data = <EventDetails>[];
      json['data'].forEach((v) {
        data!.add(EventDetails.fromJson(v));
      });
    }
    message = json['message'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['status'] = this.status;
    if (this.data != null) {
      data['data'] = this.data!.map((v) => v.toJson()).toList();
    }
    data['message'] = this.message;
    return data;
  }
}

class EventDetails {
  String? id;
  String? organizerId;
  String? gameId;
  String? gameType;
  String? perspective;
  String? title;
  String? rules;
  String? map;
  String? badge;
  String? allowedParticipants;
  String? entryFees;
  String? noOfWinner;
  String? pointsPerKill;
  String? startDate;
  String? registrationStartDate;
  List<SlotTiming>? slotTiming;
  String? prizepool;
  String? tableImg;
  List<PriceCriteria>? priceCriteria;
  dynamic winnerDate;
  String? status;
  String? createdAt;
  String? organizerName;
  String? gameName;
  String? bannerImg;
  String? joinedPlayers;
  String? remainingPlayer;
  String? remainingInPercent;

  // Only for My Events.
  String? joinedSquad;
  String? remainingSquad;
  String? occupiedDays;
  List<Stats>? stats;

  // Only for Joined Events.
  String? slotStartTime;
  String? slotEndTime;
  String? roomId;
  String? password;

  // Common in Joined & My Events.
  List<Players>? players;

  EventDetails({
    this.id,
    this.organizerId,
    this.gameId,
    this.gameType,
    this.perspective,
    this.title,
    this.rules,
    this.map,
    this.badge,
    this.allowedParticipants,
    this.entryFees,
    this.noOfWinner,
    this.pointsPerKill,
    this.startDate,
    this.registrationStartDate,
    this.slotTiming,
    this.prizepool,
    this.tableImg,
    this.priceCriteria,
    this.winnerDate,
    this.status,
    this.createdAt,
    this.slotStartTime,
    this.slotEndTime,
    this.roomId,
    this.password,
    this.organizerName,
    this.gameName,
    this.bannerImg,
    this.joinedPlayers,
    this.remainingPlayer,
    this.remainingInPercent,
    this.players,
    this.joinedSquad,
    this.remainingSquad,
    this.occupiedDays,
    this.stats,
  });

  EventDetails.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    organizerId = json['organizer_id'];
    gameId = json['game_id'];
    gameType = json['game_type'];
    perspective = json['perspective'];
    title = json['title'];
    rules = json['rules'];
    map = json['map'];
    badge = json['badge'];
    allowedParticipants = json['allowed_participants'];
    entryFees = json['entry_fees'];
    noOfWinner = json['no_of_winner'];
    pointsPerKill = json['points_per_kill'];
    startDate = json['start_date'];
    registrationStartDate = json['registration_start_date'];
    if (json['slot_timing'] != null) {
      slotTiming = <SlotTiming>[];
      json['slot_timing'].forEach((v) {
        slotTiming!.add(SlotTiming.fromJson(v));
      });
    }
    prizepool = json['prizepool'];
    tableImg = json['table_img'];
    if (json['price_criteria'] != null) {
      priceCriteria = <PriceCriteria>[];
      json['price_criteria'].forEach((v) {
        priceCriteria!.add(PriceCriteria.fromJson(v));
      });
    }
    winnerDate = json['winner_date'];
    status = json['status'];
    createdAt = json['created_at'];
    slotStartTime = json['slot_start_time'];
    slotEndTime = json['slot_end_time'];
    roomId = json['room_id'];
    password = json['password'];
    organizerName = json['organizer_name'];
    gameName = json['game_name'];
    bannerImg = json['banner_img'];
    joinedPlayers = json['joined_players'];
    remainingPlayer = json['remaining_player'];
    remainingInPercent = json['remaining_in_percent'];
    if (json['players'] != null) {
      players = <Players>[];
      json['players'].forEach((v) {
        players!.add(Players.fromJson(v));
      });
    }
    joinedSquad = json['joined_squad'];
    remainingSquad = json['remaining_squad'];
    occupiedDays = json['occupied_days'];
    if (json['stats'] != null) {
      stats = <Stats>[];
      json['stats'].forEach((v) {
        stats!.add(Stats.fromJson(v));
      });
    }
   
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['id'] = this.id;
    data['organizer_id'] = this.organizerId;
    data['game_id'] = this.gameId;
    data['game_type'] = this.gameType;
    data['perspective'] = this.perspective;
    data['title'] = this.title;
    data['rules'] = this.rules;
    data['map'] = this.map;
    data['badge'] = this.badge;
    data['allowed_participants'] = this.allowedParticipants;
    data['entry_fees'] = this.entryFees;
    data['no_of_winner'] = this.noOfWinner;
    data['points_per_kill'] = this.pointsPerKill;
    data['start_date'] = this.startDate;
    data['registration_start_date'] = this.registrationStartDate;
    if (this.slotTiming != null) {
      data['slot_timing'] = this.slotTiming!.map((v) => v.toJson()).toList();
    }
    data['prizepool'] = this.prizepool;
    data['table_img'] = this.tableImg;
    if (this.priceCriteria != null) {
      data['price_criteria'] =
          this.priceCriteria!.map((v) => v.toJson()).toList();
    }
    data['winner_date'] = this.winnerDate;
    data['status'] = this.status;
    data['created_at'] = this.createdAt;
    data['slot_start_time'] = this.slotStartTime;
    data['slot_end_time'] = this.slotEndTime;
    data['room_id'] = this.roomId;
    data['password'] = this.password;
    data['organizer_name'] = this.organizerName;
    data['game_name'] = this.gameName;
    data['banner_img'] = this.bannerImg;
    data['joined_players'] = this.joinedPlayers;
    data['remaining_player'] = this.remainingPlayer;
    data['remaining_in_percent'] = this.remainingInPercent;
    if (this.players != null) {
      data['players'] = this.players!.map((v) => v.toJson()).toList();
    }
    data['joined_squad'] = this.joinedSquad;
    data['remaining_squad'] = this.remainingSquad;
    data['occupied_days'] = this.occupiedDays;
    if (this.stats != null) {
      data['stats'] = this.stats!.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class SlotTiming {
  String? id;
  String? slotStartTime;
  String? slotEndTime;

  SlotTiming({this.id, this.slotStartTime, this.slotEndTime});

  SlotTiming.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    slotStartTime = json['slot_start_time'];
    slotEndTime = json['slot_end_time'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['slot_start_time'] = this.slotStartTime;
    data['slot_end_time'] = this.slotEndTime;
    return data;
  }
}

class PriceCriteria {
  String? rank;
  String? to;
  String? amount;

  PriceCriteria({this.rank, this.to, this.amount});

  PriceCriteria.fromJson(Map<String, dynamic> json) {
    rank = json['rank'];
    to = json['to'];
    amount = json['amount'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['rank'] = this.rank;
    data['to'] = this.to;
    data['amount'] = this.amount;
    return data;
  }
}

class Players {
  String? name;
  String? mobile;
  String? id;
  String? rank;
  String? kills;
  String? points;

  Players(
      {this.name, this.mobile, this.id, this.rank, this.kills, this.points});

  Players.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    mobile = json['mobile'];
    id = json['id'];
    rank = json['rank'];
    kills = json['kills'];
    points = json['points'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['name'] = this.name;
    data['mobile'] = this.mobile;
    data['id'] = this.id;
    data['rank'] = this.rank;
    data['kills'] = this.kills;
    data['points'] = this.points;
    return data;
  }
}

class Stats {
  String? totalPaid;
  String? entryFee;
  String? prizepool;
  String? remainingAmount;
  String? totalEarning;

  Stats(
      {this.totalPaid,
      this.entryFee,
      this.prizepool,
      this.remainingAmount, 
      this.totalEarning});

  Stats.fromJson(Map<String, dynamic> json) {
    totalPaid = json['total_paid'];
    entryFee = json['entry_fee'];
    prizepool = json['prizepool'];
    remainingAmount = json['remaining_amount'];
    totalEarning = json['total_earning'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['total_paid'] = this.totalPaid;
    data['entry_fee'] = this.entryFee;
    data['prizepool'] = this.prizepool;
    data['remaining_amount'] = this.remainingAmount;
    data['total_earning'] = this.totalEarning;
    return data;
  }
}
Was this helpful?