Home > Back-end >  How to deserialize the dynamic key json in flutter?
How to deserialize the dynamic key json in flutter?

Time:03-25

I want to deserialize the JSON file but I have no idea how to build a model from this file. I want to know if my code is right or wrong

{
  "Thriller": [
    {
      "Death Clique": "https://www.youtube.com/watch?v=4Q9lTjqQeBU&ab_channel=VMovies"
    },
    {
      "CID": "https://www.youtube.com/watch?v=4Q9lTjqQeBU&ab_channel=VMovies"
    },
    {
      "Wrong Turn": "https://www.youtube.com/watch?v=9spc-dExLH0&ab_channel=SagarForever"
    },
    
  ],
  "Action Movie": [
    {
      "Nobody": "https://nobodymovie.com"
    },
    {
      "Tenet": "https://tenetmovie.com"
    },
    
  ],
  "Romantic Movie": [
    {
      "Titanic": "https://titanicmovie.com"
    },
    {
      "The Vow": "https://thevowmovie.com"
    },
    
  ]
}

I have built this model to fetch keys like (thriller, action movie,) to display in the list and inside list to display the values (movie name) in grid view.

class MovieCategories {
  final String? title;
  final List<Movie>? subServices;

  MovieCategories({ this.title, this.subServices});

  factory MovieCategories.fromJson(Map<String, dynamic> json) {
     var list = json.values as List;
    List<Movie> imagesList = list.map((i) => Movie.fromJson(i)).toList();

    return MovieCategories(
      title: json.keys.toString(),
      subServices: imagesList,
    );
  }
}
class Movie {
  final String? title;
  final String? url;

  Movie({this.title, this.url});

  factory Movie.fromJson(Map<String, dynamic> parsedJson){
   return Movie(
     title:parsedJson.keys.toString(),
     url:parsedJson.values.toString()
   );
  }
}

CodePudding user response:

If your data are constant like that then you can try this code.

import 'dart:convert';

/// Map<String, List<Map<String, String>>>
class MovieCategories {
  final String? title;
  final List<Movie>? subServices;

  MovieCategories({this.title, this.subServices});

  factory MovieCategories.fromJson(
      String cat, List<Map<String, String>>? movies) {
    return MovieCategories(
      title: cat,
      subServices: movies?.map((m) => Movie.fromJson(m)).toList(),
    );
  }

  static List<MovieCategories> fromJsonMaps(String jsonString) {
    final _listOfMovies =
        Map<String, List<Map<String, String>>>.from(jsonDecode(jsonString));
    return _listOfMovies.keys
        .map((cat) => MovieCategories.fromJson(cat, _listOfMovies[cat]))
        .toList();
  }
}

class Movie {
  final String? title;
  final String? url;

  Movie({this.title, this.url});

  factory Movie.fromJson(Map<String, String> parsedJson) {
    return Movie(title: parsedJson.keys.first, url: parsedJson.values.first);
  }
}

CodePudding user response:

I'd also recommend following the proper key-value-pair JSON convention, as in:

{
  [
   {
    "genre":"thriller",
    "movies": [
      {
        "title": "Nobody",
        "url": "https://nobodymovie.com"
      },
      {
        "title": "Tenet",
        "url": "https://tenetmovie.com"
      }
    ]
    ...
  }

That way the mapping would be much more straight-forward:

class Movie {
  final String? title;
  final String? url;

  Movie({this.title, this.url});

  factory Movie.fromJson(Map<String, dynamic> parsedJson){
   return Movie(
     title: parsedJson['title'],
     url: parsedJson['url']
   );
  }
}

And your MovieCategories*:

class MovieCategories {
  final String? genre;
  final List<Movie>? movies;

  MovieCategories({this.genre, this.movies});

  factory MovieCategories.fromJson(
      String cat, <Map<String, String> genre) {
    return MovieCategories(
      genre: genre['genre'],
      movies: genre['movies'].map((m) => Movie.fromJson(m)).toList(),
    );
  }
}
  • Related