[Solved] How to convert JSON to object in Flutter? [closed]


Maybe, this website is what you’re looking for:
https://javiercbk.github.io/json_to_dart/

You simply paste your json (usually the response coming from an API) and it creates a class in dart for that json. so then, you can use it like:

MyJsonClass classResponse = MyJsonClass.fromJson(apiResponse);

And access its parameters like this:

print(classResponse.mon.a) // output: 3

generated code used:

class Days {
  Mon mon;
  Mon tue;
  Mon wed;
  Mon sun;

  Days({this.mon, this.tue, this.wed, this.sun});

  Days.fromJson(Map<String, dynamic> json) {
    mon = json['mon'] != null ? new Mon.fromJson(json['mon']) : null;
    tue = json['tue'] != null ? new Mon.fromJson(json['tue']) : null;
    wed = json['wed'] != null ? new Mon.fromJson(json['wed']) : null;
    sun = json['sun'] != null ? new Mon.fromJson(json['sun']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.mon != null) {
      data['mon'] = this.mon.toJson();
    }
    if (this.tue != null) {
      data['tue'] = this.tue.toJson();
    }
    if (this.wed != null) {
      data['wed'] = this.wed.toJson();
    }
    if (this.sun != null) {
      data['sun'] = this.sun.toJson();
    }
    return data;
  }
}

class Mon {
  int a;
  int b;
  int c;

  Mon({this.a, this.b, this.c});

  Mon.fromJson(Map<String, dynamic> json) {
    a = json['a'];
    b = json['b'];
    c = json['c'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['a'] = this.a;
    data['b'] = this.b;
    data['c'] = this.c;
    return data;
  }
}

3

solved How to convert JSON to object in Flutter? [closed]