I am currently trying to parse this json string:
{"code":200,"status":"OK","results":{"datetime":[{"times":{"Imsak":"05:21","Sunrise":"06:43","Fajr":"05:31","Dhuhr":"12:32","Asr":"15:51","Sunset":"18:22","Maghrib":"18:32","Isha":"19:33","Midnight":"23:57"},"date":{"timestamp":1640736000,"gregorian":"2021-12-29","hijri":"1443-05-25"}}],"location":{"latitude":10.516667366027832,"longitude":7.433332920074462,"elevation":611.0,"city":"Kaduna","country":"Nigeria","country_code":"NG","timezone":"Africa/Lagos","local_offset":1.0},"settings":{"timeformat":"HH:mm","school":"Ithna Ashari","juristic":"Shafii","highlat":"None","fajr_angle":18.0,"isha_angle":18.0}}}
I created this class:
class ParseJSON {
ParseJSON({
this.imsak,
this.sunrise,
this.fajr,
this.dhuhr,
this.asr,
this.sunset,
this.maghrib,
this.isha,
this.midnight,
});
String imsak;
String sunrise;
String fajr;
String dhuhr;
String asr;
String sunset;
String maghrib;
String isha;
String midnight;
factory ParseJSON.fromJson(Map<String, dynamic> json) => ParseJSON(
imsak: json["Imsak"],
sunrise: json["Sunrise"],
fajr: json["Fajr"],
dhuhr: json["Dhuhr"],
asr: json["Asr"],
sunset: json["Sunset"],
maghrib: json["Maghrib"],
isha: json["Isha"],
midnight: json["Midnight"],
);
Map<String, dynamic> toJson() => {
"Imsak": imsak,
"Sunrise": sunrise,
"Fajr": fajr,
"Dhuhr": dhuhr,
"Asr": asr,
"Sunset": sunset,
"Maghrib": maghrib,
"Isha": isha,
"Midnight": midnight,
};
}
I tried to access the contents of the json thus:
Future<List<ParseJSON>> fetchJSON() async {
String jsonResponse = """
{"code":200,"status":"OK","results":{"datetime":[{"times":{"Imsak":"05:21","Sunrise":"06:43","Fajr":"05:31","Dhuhr":"12:32","Asr":"15:51","Sunset":"18:22","Maghrib":"18:32","Isha":"19:33","Midnight":"23:57"},"date":{"timestamp":1640736000,"gregorian":"2021-12-29","hijri":"1443-05-25"}}],"location":{"latitude":10.516667366027832,"longitude":7.433332920074462,"elevation":611.0,"city":"Kaduna","country":"Nigeria","country_code":"NG","timezone":"Africa/Lagos","local_offset":1.0},"settings":{"timeformat":"HH:mm","school":"Ithna Ashari","juristic":"Shafii","highlat":"None","fajr_angle":18.0,"isha_angle":18.0}}}
""";
ParseJSON welcomeFromJson(String str) => ParseJSON.fromJson(json.decode(str));
final welcome = welcomeFromJson(jsonResponse);
print("Printing contents from json string");
//print(jsonResponse);
print(welcome.maghrib);
print(welcome.fajr);
}
The problem is that both print(welcome.maghrib) and print(welcome.fajr) return null. Please what am i doing incorrectly? Any help would be appreciated. Thanks