Flutter 索引 Json 数据
Flutter Indexing Json Datas
我不想像 (json["features"][0]
或 json["features"][1])
那样索引 json。我怎样才能将其改为列表或其他内容?
我的代码如下:
class Other {
String title;
double mag;
String title1;
double mag1;
String title2;
double mag2;
Other({this.mag, this.title, this.mag1, this.title1, this.mag2, this.title2});
factory Other.fromJson(Map<String, dynamic> json) {
return Other(
title: json["features"][0]["properties"]["place"],
mag: json["features"][0]["properties"]["mag"],
title1: json["features"][1]["properties"]["place"],
mag1: json["features"][1]["properties"]["mag"],
title2: json["features"][2]["properties"]["place"],
mag2: json["features"][2]["properties"]["mag"],
);
}
}
json 数据在这里:https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson
您可以重构您的代码:
factory Other.fromJson(Map<String, dynamic> json) {
final features = json['features'];
final firstProperties = reatures[0]['properties'];
final secondProperties = reatures[0]['properties'];
final thirdProperties = reatures[0]['properties'];
return Other(
title: firstProperties['place'],
mag: firstProperties['mag'],
title1: secondProperties['place'],
mag1: secondProperties['mag'],
title2: thirdProperties['place'],
mag2: thirdProperties['mag'],
);
}
或者使用像 this one 这样的插件为您的实体生成 JSON 解析器。
有关如何使用 JSON 的更多信息包含在 Official Flutter Documentation 中。
我不想像 (json["features"][0]
或 json["features"][1])
那样索引 json。我怎样才能将其改为列表或其他内容?
我的代码如下:
class Other {
String title;
double mag;
String title1;
double mag1;
String title2;
double mag2;
Other({this.mag, this.title, this.mag1, this.title1, this.mag2, this.title2});
factory Other.fromJson(Map<String, dynamic> json) {
return Other(
title: json["features"][0]["properties"]["place"],
mag: json["features"][0]["properties"]["mag"],
title1: json["features"][1]["properties"]["place"],
mag1: json["features"][1]["properties"]["mag"],
title2: json["features"][2]["properties"]["place"],
mag2: json["features"][2]["properties"]["mag"],
);
}
}
json 数据在这里:https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson
您可以重构您的代码:
factory Other.fromJson(Map<String, dynamic> json) {
final features = json['features'];
final firstProperties = reatures[0]['properties'];
final secondProperties = reatures[0]['properties'];
final thirdProperties = reatures[0]['properties'];
return Other(
title: firstProperties['place'],
mag: firstProperties['mag'],
title1: secondProperties['place'],
mag1: secondProperties['mag'],
title2: thirdProperties['place'],
mag2: thirdProperties['mag'],
);
}
或者使用像 this one 这样的插件为您的实体生成 JSON 解析器。
有关如何使用 JSON 的更多信息包含在 Official Flutter Documentation 中。