2

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.

2 Answers 2

1

It is the complete code if someone need it, Thanks KURRU HEM.

    Map<String, dynamic> jsonParsed = {"2019-01-15":{"title":"Hey","nbLike":1}, "2019-01-16":{"title":"Hey2","nbLike":2}};
    print(jsonParsed);
 List<Event> _events = [];
List _dates = [];
jsonParsed.keys.forEach((String key){
   _dates.add(key);
});
  print(_dates);
for(int i=0; i<_dates.length; i++){
  print(jsonParsed[_dates[i]]['title']);
  print(jsonParsed[_dates[i]]['nbLike']);
   final Event event = Event(
      title: jsonParsed[_dates[i]]['title'],
      nbLike: jsonParsed[_dates[i]]['nbLike'],
);
    _events.add(event);
}
print('EVENTS --------------> $_events');


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,
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this.

List _events = [];
List _dates = [];
jsonParsed.keys.forEach((String key){
   _dates.add(key);
});
for(int i=0; i<_dates.length; i++){
  jsonParsed[_date[i]].forEach((event){
   final Event event = Event(
      title: jsonParsed['title'],
      nbLike: jsonParsed['nbLike'],

    );
    _events.add(event);
  });
}

4 Comments

i have this error for jsonParsed.keys : [dart] The getter 'keys' isn't defined for the class 'List'. [undefined_getter]
Change the paredJson to type Map
I change it but foreach can't accept key as string, so i change to dynamic. The code dont show errors but flutter give me this error : type 'List<dynamic>' is not a subtype of type 'Map<dynamic, dynamic>', sorry iam new with flutter
Map<String, dynamic> jsonParsed = {}; Try this. I worked similar type of json format. I first stored the keys in list and later extracted the details using the keys stored in the list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.