我想在回應體中獲取特定的子體。
我想在回應體中獲取特定的子體。
final response = await http.get(Uri.parse(url)>)。
var json = jsonDecode(response.body)。
print(json)。
輸出 :
{
"country"/span> : {
"name"/span> : "USA"/span>
},
"league" : {
"name" : "籃球杯"。
"type" : "Cup".
},
"seasons" : [ {
"current" : false,
"end" : "2009-06-28",
"start" : "2009-06-14",
"年份" : 2009"current" : false,
"end" : "2013-06-30",
"start" : "2013-06-15",
"年份" : 2013"current" : true,
"end" : "2017-07-02"。
"start" : "2017-06-17",
"year" : 2017, "year"
} ]
如何只獲得主體 "季節"?
uj5u.com熱心網友回復:
編輯過
讓我們試試吧
void main() {
var data =
{
"country" : {
"name" : "USA" "league" : {
"name" : "籃球杯"。
"type" : "Cup".
},
"seasons" : [ {
"current" : false,
"end" : "2009-06-28",
"start" : "2009-06-14",
"年份" : 2009"current" : false,
"end" : "2013-06-30",
"start" : "2013-06-15",
"年份" : 2013"current" : true,
"end" : "2017-07-02"。
"start" : "2017-06-17",
"year" : 2017, "year"
} ]
};
FootballData datas = FootballData.fromJson(data);
datas.seasons.forEach((e)=>print(e.year))。
}
class FootballData{
國家 country;
聯盟league。
List<Seasons> seasons;
FootballData({this.country, this.league, this.seasons}) 。
FootballData.fromJson(Map<String, dynamic> json) {
國家 =
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" : "籃球杯"。
"type" : "Cup".
},
"seasons" : [ {
"current" : false,
"end" : "2009-06-28",
"start" : "2009-06-14",
"年份" : 2009"current" : false,
"end" : "2013-06-30",
"start" : "2013-06-15",
"年份" : 2013"current" : true,
"end" : "2017-07-02"。
"start" : "2017-06-17",
"year" : 2017, "year"
} ]
};
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;
}
//輸出
2009
2013
2017
uj5u.com熱心網友回復:
考慮到你有以下json字串:
const x = {
"country"/span> : {
"name" : "USA" "league" : {
"name" : "籃球杯"。
"type" : "Cup".
},
"seasons" : [ {
"current" : false,
"end" : "2009-06-28",
"start" : "2009-06-14",
"年份" : 2009"current" : false,
"end" : "2013-06-30",
"start" : "2013-06-15",
"年份" : 2013"current" : true,
"end" : "2017-07-02"。
"start" : "2017-06-17",
"year" : 2017, "year"
} ]
};
String jsonFile = json.encode(x);
現在你可以訪問seasons部分,如下:
Map<String, dynamic> data = json.decode(jsonFile) 。
List<dynamic> seasons = data["seasons"] 。
現在你有一個季節的串列,例如:
你有一個季節的串列。
print(seasons[0]["current"]) 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/311860.html
標籤:
上一篇:如何將灰度影像變成RGB?
