如何在颤动中获得 JSON 映射的第一个值?
Posted
技术标签:
【中文标题】如何在颤动中获得 JSON 映射的第一个值?【英文标题】:How do i get the first value of the JSON map in flutter? 【发布时间】:2020-08-01 20:57:00 【问题描述】:
"Meta Data":
"1. Information": "Daily Time Series with Splits and Dividend Events",
"2. Symbol": "IBM",
"3. Last Refreshed": "2020-04-17",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
,
"Time Series (Daily)":
"2020-04-17":
"1. open": "119.3000",
"2. high": "120.3900",
"3. low": "117.9200",
"4. close": "120.1200",
"5. adjusted close": "120.1200",
"6. volume": "4944745",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
,
"2020-04-16":
"1. open": "119.0100",
"2. high": "119.7500",
"3. low": "114.4200",
"4. close": "115.7300",
"5. adjusted close": "115.7300",
"6. volume": "6438128",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
,
这是我的代码
JSONObject json = await timeSeries.getDaily(stock);
var data = json.getJSONMap()["Time Series (Daily)"];
var data2 = Map<String, dynamic>.from(data);
data2.forEach((k, v) => open.add(double.parse(v["1. open"])));
data2.forEach((k, v) => close.add(double.parse(v["4. close"])));
我只想获得第一个高点和第一个低点。 119.300 和 120.390。 显示的代码有效,但不是每个 V,我只想得到第一个。
【问题讨论】:
【参考方案1】:试试这个
JSONObject json = await timeSeries.getDaily(stock);
var data = json.getJSONMap()["Time Series (Daily)"];
var data2 = Map<String, dynamic>.from(data);
var firstKey = data2.keys.toList()[0];
open.add(data2[firstKey]["1. open"]);
close.add(data2[firstKey]["4. close"]);
【讨论】:
以上是关于如何在颤动中获得 JSON 映射的第一个值?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 PostgreSQL 中获取表的每一天的第一个日期并将其转换为 JSON