Alvin Heng
Alvin Heng

Reputation: 35

How do i get the first value of the JSON map in flutter?

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

Here is my code

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"])));

i only want to get the first high, and the first low. 119.300 and 120.390. The code shown works, but instead of for every V, i want to get just the first one.

Upvotes: 1

Views: 957

Answers (1)

Josteve Adekanbi
Josteve Adekanbi

Reputation: 12693

Try this

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"]);

Upvotes: 1

Related Questions