Flutter:如何达到特定的 JSON body 的 children

Flutter: How to reach specific JSON body's children

我正在尝试在响应 body 中获取特定 children 的 body。

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

输出:

{
  "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
  } ]
}

如何只获取 body ‘seasons’?

已编辑

我们来试试

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;
  }
}

另一种方法

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

考虑一下您有以下 json 字符串:

const x = {
  "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
  } ]
};
    
  String jsonFile = json.encode(x);

现在您可以按如下方式访问 seasons 部分:

Map<String, dynamic> data = json.decode(jsonFile);
  List<dynamic> seasons = data["seasons"];

现在您有了季节列表,例如:

print(seasons[0]["current"]);