Search code examples
jsonflutterhttpresponsejsondecoder

Flutter: How to reach specific JSON body's children


I'm trying to get the body of specific children inside the response body.

final response = await http.get(Uri.parse(url));
var json = jsonDecode(response.body);
print(json);

Output :

{
  "country" : {
    "name" : "USA"
  },
  "league" : {
    "name" : "Basketball Cup",
    "type" : "Cup"
  },
  "seasons" : [ {
    "current" : false,
    "end" : "2009-06-28",
    "start" : "2009-06-14",
    "year" : 2009
  }, {
    "current" : false,
    "end" : "2013-06-30",
    "start" : "2013-06-15",
    "year" : 2013
  }, {
    "current" : true,
    "end" : "2017-07-02",
    "start" : "2017-06-17",
    "year" : 2017
  } ]
}

How to get the body ‘seasons’ only?


Solution

  • Edited

    Let's try

    void main() {
    var data =
             {
      "country" : {
        "name" : "USA"
      },
      "league" : {
        "name" : "Basketball Cup",
        "type" : "Cup"
      },
      "seasons" : [ {
        "current" : false,
        "end" : "2009-06-28",
        "start" : "2009-06-14",
        "year" : 2009
      }, {
        "current" : false,
        "end" : "2013-06-30",
        "start" : "2013-06-15",
        "year" : 2013
      }, {
        "current" : true,
        "end" : "2017-07-02",
        "start" : "2017-06-17",
        "year" : 2017
      } ]
    };
      FootballData datas = FootballData.fromJson(data);
      datas.seasons.forEach((e)=>print(e.year));
    
    }
    class FootballData {
      Country country;
      League league;
      List<Seasons> seasons;
    
      FootballData({this.country, this.league, this.seasons});
    
      FootballData.fromJson(Map<String, dynamic> json) {
        country =
            json['country'] != null ? new Country.fromJson(json['country']) : null;
        league =
            json['league'] != null ? new League.fromJson(json['league']) : null;
        if (json['seasons'] != null) {
          seasons = new List<Seasons>();
          json['seasons'].forEach((v) {
            seasons.add(new Seasons.fromJson(v));
          });
        }
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.country != null) {
          data['country'] = this.country.toJson();
        }
        if (this.league != null) {
          data['league'] = this.league.toJson();
        }
        if (this.seasons != null) {
          data['seasons'] = this.seasons.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }
    
    class Country {
      String name;
    
      Country({this.name});
    
      Country.fromJson(Map<String, dynamic> json) {
        name = json['name'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['name'] = this.name;
        return data;
      }
    }
    
    class League {
      String name;
      String type;
    
      League({this.name, this.type});
    
      League.fromJson(Map<String, dynamic> json) {
        name = json['name'];
        type = json['type'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['name'] = this.name;
        data['type'] = this.type;
        return data;
      }
    }
    
    class Seasons {
      bool current;
      String end;
      String start;
      int year;
    
      Seasons({this.current, this.end, this.start, this.year});
    
      Seasons.fromJson(Map<String, dynamic> json) {
        current = json['current'];
        end = json['end'];
        start = json['start'];
        year = json['year'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['current'] = this.current;
        data['end'] = this.end;
        data['start'] = this.start;
        data['year'] = this.year;
        return data;
      }
    }
    

    Another approach

    void main() {
    Map data =
             {
      "country" : {
        "name" : "USA"
      },
      "league" : {
        "name" : "Basketball Cup",
        "type" : "Cup"
      },
      "seasons" : [ {
        "current" : false,
        "end" : "2009-06-28",
        "start" : "2009-06-14",
        "year" : 2009
      }, {
        "current" : false,
        "end" : "2013-06-30",
        "start" : "2013-06-15",
        "year" : 2013
      }, {
        "current" : true,
        "end" : "2017-07-02",
        "start" : "2017-06-17",
        "year" : 2017
      } ]
    };
    List<Seasons> seasons = data["seasons"]
              .map<Seasons>((x) => Seasons.fromJson(x))
              .toList();
      seasons.forEach((e){print(e.year);});
    }
    class Seasons {
      bool current;
      String end;
      String start;
      int year;
    
      Seasons({this.current, this.end, this.start, this.year});
    
      Seasons.fromJson(Map<String, dynamic> json) {
        current = json['current'];
        end = json['end'];
        start = json['start'];
        year = json['year'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['current'] = this.current;
        data['end'] = this.end;
        data['start'] = this.start;
        data['year'] = this.year;
        return data;
      }
    }
    // output
    2009
    2013
    2017