I'm new to Flutter and doesn't know much about complex JSON parsing. I've consulted few online articles but didn't find any suitable solution for the case. My JSON is as follows:
{
"RIFUSD":[
{
"timestamp":"2021-02-13T16:00:00.000Z",
"open":"0.3257370",
"close":"0.3257370",
"min":"0.3257370",
"max":"0.3257370",
"volume":"49",
"volumeQuote":"15.9611130"
},
{
"timestamp":"2021-02-13T12:00:00.000Z",
"open":"0.3015120",
"close":"0.3216128",
"min":"0.3015120",
"max":"0.3216768",
"volume":"4079",
"volumeQuote":"1298.0319504"
}
],
"BERRYUSD":[
{
"timestamp":"2021-02-13T04:00:00.000Z",
"open":"0.00061800",
"close":"0.00061780",
"min":"0.00061000",
"max":"0.00071783",
"volume":"10460",
"volumeQuote":"6.89477840"
},
{
"timestamp":"2021-02-12T20:00:00.000Z",
"open":"0.00060489",
"close":"0.00061800",
"min":"0.00048829",
"max":"0.00061800",
"volume":"466690",
"volumeQuote":"228.12405820"
}
]
}
And my Candle Class to keep timestamp, open, close, min, max, volume and volumeQuote:
class Candle {
Candle({
this.timestamp,
this.open,
this.close,
this.min,
this.max,
this.volume,
this.volumeQuote,
});
final DateTime timestamp;
final String open;
final String close;
final String min;
final String max;
final String volume;
final String volumeQuote;
factory Candle.fromRawJson(String str) => Candle.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory Candle.fromJson(Map<String, dynamic> json) => Candle(
timestamp: json["timestamp"] == null
? null
: DateTime.parse(json["timestamp"]),
open: json["open"] == null ? null : json["open"],
close: json["close"] == null ? null : json["close"],
min: json["min"] == null ? null : json["min"],
max: json["max"] == null ? null : json["max"],
volume: json["volume"] == null ? null : json["volume"],
volumeQuote: json["volumeQuote"] == null ? null : json["volumeQuote"],
);
Map<String, dynamic> toJson() => {
"timestamp": timestamp == null ? null : timestamp.toIso8601String(),
"open": open == null ? null : open,
"close": close == null ? null : close,
"min": min == null ? null : min,
"max": max == null ? null : max,
"volume": volume == null ? null : volume,
"volumeQuote": volumeQuote == null ? null : volumeQuote,
};
}
I want to parse this JSON into this:
class CandleList {
CandleList({
this.candleList,
this.symbol,
});
final List<Candle> candleList;
final Sym symbol;
factory CandleList.fromRawJson(String str) =>
CandleList.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory CandleList.fromJson(Map<String, dynamic> json) => CandleList(
candleList: json["candleList"] == null
? null
: List<Candle>.from(
json["candleList"].map((x) => Candle.fromJson(x))),
symbol: json["symbol"] == null ? null : Sym.fromJson(json["symbol"]),
);
Map<String, dynamic> toJson() => {
"candleList": candleList == null
? null
: List<dynamic>.from(candleList.map((x) => x.toJson())),
"symbol": symbol == null ? null : symbol.toJson(),
};
@override
String toString() {
return '${symbol.id} > ${candleList.length} Candles';
}
}
With List and "RIFUSD","BERRYUSD" as symbol.id of CandleList class.