![](/img/trans.png)
[英]Flutter - How to parse nested JSON structures with lists in flutter?
[英]Flutter: How to parse json nested map on dart
这是我要解析的 json 文件:
{
"Meta Data": {
"1. Information": "Weekly Adjusted Prices and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2020-03-25",
"4. Time Zone": "US/Eastern"
},
"Weekly Adjusted Time Series": {
"2020-03-25": {
"1. open": "137.0100",
"2. high": "154.3300",
"3. low": "132.5200",
"4. close": "146.9200",
"5. adjusted close": "146.9200",
"6. volume": "235583286",
"7. dividend amount": "0.0000"
},
"2020-03-20": {
"1. open": "140.0000",
"2. high": "150.1500",
"3. low": "135.0000",
"4. close": "137.3500",
"5. adjusted close": "137.3500",
"6. volume": "421347734",
"7. dividend amount": "0.0000"
},
}
我只是想知道日期,打开和关闭。 我尝试在网上找东西,但仍然无法这样做。
这是我的 TimeSeries 课程:
class TimeSeries {
final String date;
final double open;
final double close;
TimeSeries({this.date, this.open, this.close});
factory TimeSeries.fromJson(Map<String, dynamic> json) {
return TimeSeries(
date: json[''],
open: double.parse(json['1. open']),
close: double.parse(json['4. close']),
);
}
}
这些是我现在的功能:
Future getTimeSeries(String stock) async {
print("Starting get request");
http.get("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo").then((res){
print("received response.");
var resObj = json.decode(res.body);
}).catchError((e) {
print("Failed to get response.");
});
}
你可能想在这里做一些映射。 你的TimeSeries
类就足够了,但你需要调整你的getTimeSeries
函数。 首先,您可能希望更改其返回类型以返回带有List<TimeSeries>
的Future
,因为您将获得这些列表。
在您的 HTTP 调用的then
回调中,您希望首先进入您有数据的 JSON 映射,因此在这种情况下,它是resObj['Weekly Adjusted Time Series']
。 然后,在此运行map
,这是您想要实例化TimeSeries
的地方(我还使用了final
而不是var
- 在不需要var
地方使用它只是一个好习惯):
final resObj = json.decode(res.body);
return resObj.map((date, fields) {
return TimeSeries(DateTime.parse(date), fields['1. open'], fields['4. close']);
});
IMO TimeSeries.fromJson
在这里无济于事,因为您没有从 JSON 中的此内部地图中获取date
字段。
您的全新代码如下所示:
class TimeSeries {
final String date;
final double open;
final double close;
TimeSeries({this.date, this.open, this.close});
}
Future<List<TimeSeries>> getTimeSeries(String stock) async {
print("Starting get request");
return http.get("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo").then((res){
print("received response.");
final resObj = json.decode(res.body);
return resObj.map((date, fields) {
return TimeSeries(date, fields['1. open'], fields['4. close']);
});
}).catchError((e) {
print("Failed to get response.");
});
}
尝试这个。
class TimeSeries {
final String date;
final double open;
final double close;
TimeSeries({this.date, this.open, this.close});
factory TimeSeries.fromJson(Map<String, dynamic> json, String indexDate) {
return TimeSeries(
date: indexDate,
open: double.parse(json["Weekly Adjusted Time Series"][indexDate]['1. open']),
close: double.parse(json["Weekly Adjusted Time Series"][indexDate]['4. close']),
);
}
}
您可以按如下方式调用构造函数
List<TimeSeries> datesList(json) {
List<TimeSeries> dates;
for (date in json["Weekly Adjusted Time Series"]) {
dates.add(TimeSeries.fromJson(json, date));
}
return dates;
}
我建议使用这个库https://github.com/k-paxian/dart-json-mapper
当然,如果您不喜欢手动为所有模型类硬编码.fromJson
方法,并且不喜欢调试和稍后支持它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.