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()
);
}
}