How to Deserialize a list of objects from json in flutter

后端 未结 8 1023
无人共我
无人共我 2020-12-04 19:30

I am using the dart package json_serializable for json serialization. Looking at the flutter documentation it shows how to deserialize a single object as follow:

<         


        
相关标签:
8条回答
  • 2020-12-04 19:41

    You can also Do it like

      List< Item > itemsList= List< Item >.from(parsedListJson.map((i) => Item.fromJson(i)));
    
    0 讨论(0)
  • 2020-12-04 19:43

    This is my Model class -

      class SuggestedMovie {
      String title;
      String genres;
      int movieId;
      SuggestedMovie({this.title, this.genres, this.movieId});
      factory SuggestedMovie.fromJson(Map<dynamic, dynamic> parsedJson) {
        return SuggestedMovie(
          movieId: parsedJson['movieId'],
          title: parsedJson['title'] as String,
          genres: parsedJson['genres'] as String,
        );
      }
    }
    

    The one below is the code for Deserializing the JSON response into List

     suggestedMovie = (json.decode(jsonResponse.data) as List)
          .map((i) => SuggestedMovie.fromJson(i))
          .toList();
    
    0 讨论(0)
提交回复
热议问题