I need to parse a json with dynamic arrays keys like this :
[{ key1:{ title: ....., img: ....., }, key2:{ title: ....., img: ....., }, }]
I know how to parse a json like this:
[ {title: .....,nbLike: ...}, {title: ...., nbLike: ...} ]
but i didn't find anything about parsing json with a dynamic key like this.
I tried this but it doesnt work.
class Event {
final String title;
final int nbLike;
Event({this.title, this.nbLike});
factory Event.fromJson(Map<String, dynamic> json) {
return Event(
title: json['title'] as String,
nbLike: json['nbLike'] as int,
);
}
}
Future<List<Event>> fetchPosts(http.Client client) async {
final response = '[{"2019-01-15":{"title":"Hey","nbLike":1}, "2019-01-
16":{"title":"Hey2","nbLike":2}}]';
return compute(parsePosts, response);
}
List<Event> parsePosts(String responseBody) {
List<Event> events = new List<Event>();
List jsonParsed = json.decode(responseBody.toString());
for (int i = 0; i < jsonParsed.length; i++) {
print('jsonParsed1 ${jsonParsed.length}');
print('jsonParsed ${jsonParsed[i]}');
events.add(new Event.fromJson(jsonParsed[i]));
}
return events;
}
I receive json from API with a key and i want to transform it to this
[{title: .....,nbLike: ...},{title: ...., nbLike: ...}]
to create a 'Event' list and display it in a card list.