Reputation: 33
This is the JSON data and I'm trying to access the date and the open value with this code but I'm getting errors. What would the proper syntax be?
factory Stock.fromJson(Map<String, dynamic> json) { // parse the json into data we can use
return Stock(
date: json['Time Series (Daily)[0]'],
open: json['Time Series (Daily[0].open'],
high: json['Time Series (Daily[0].high'],
low: json['Time Series (Daily[0].low'],
close: json['Time Series (Daily[0].close'],
volume: json['Time Series (Daily[0].volume']
);
}
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2020-03-26",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2020-03-26": {
"1. open": "148.4000",
"2. high": "156.6600",
"3. low": "148.3700",
"4. close": "155.8800",
"5. volume": "64143669"
}
}
Upvotes: 0
Views: 160
Reputation: 2215
To get json objects or values of your json data, you need to pass attributes names and manipulate the json object
factory Stock.fromJson(Map<String, dynamic> json) { // parse the json into data we can use
return Stock(
date: json['Time Series (Daily)']['2020-03-26'],
open: json['Time Series (Daily)']['2020-03-26']['1. open'],
...
);
}
You can also convert your json to dart object with this tool : https://javiercbk.github.io/json_to_dart/
Upvotes: 1