Tengo el siguiente JSON:
"cast": [
{
"cast_id": 0,
"character": "John Wick",
"credit_id": "591d49ad925141499001b005",
"gender": 2,
"id": 6384,
"name": "Keanu Reeves",
"order": 0,
"profile_path": "/bOlYWhVuOiU6azC4Bw6zlXZ5QTC.jpg"
},
{
"cast_id": 9,
"character": "Sofia",
"credit_id": "5b031331925141097301b798",
"gender": 1,
"id": 4587,
"name": "Halle Berry",
"order": 1,
"profile_path": "/hdUqx0on0cqbFuJCZtEGU42UWe5.jpg"
},
]
Tengo los siguientes metodos que me agarran ese JSON y lo pasan a un modelo:
final respuesta=await http.get(url);
final decodedData=json.decode(respuesta.body);
final cast=new Cast.fromJSONMap(decodedData['cast']);
La ultima linea hace referencia a este metodo en el modelo, de ahi se mapean los datos y se guardan:
Cast.fromJSONMap(List<dynamic> jsonList) {
if (jsonList == null) return;
for (var item in jsonList) {
final actor = new Actor.fromJSONMap(item);
actores.add(actor);
}
}
Hasta aqui me funciona todo bien.
Resulta que tengo este otro JSON:
{
"birthday": "1964-09-02",
"known_for_department": "Acting",
"deathday": null,
"id": 6384,
"name": "Keanu Reeves",
"also_known_as": [
"Киану Ривз",
"كيانو ريفز",
"키아누 리브스",
"キアヌ・リーブス",
"เคอานู รีฟส์",
"基努·里维斯",
"קיאנו ריבס",
"Keanu Charles Reeves"
],
"gender": 2,
"biography": "XXXXXXXXXXXX",
"popularity": 34.892,
"place_of_birth": "Beirut, Lebanon",
"profile_path": "/bOlYWhVuOiU6azC4Bw6zlXZ5QTC.jpg",
"adult": false,
"imdb_id": "nm0000206",
"homepage": null
}
Resulta que quiero recuperarlo y mapearlo como el anterior, pero este JSON no tiene un xxx:[]
que lo esta envolviendo como el ejemplo anterior, por tanto, a la hora de pasarlo para mapearlo usando la mismos metodos:
final respuesta=await http.get(url);
final decodedData=json.decode(respuesta.body);
final person=new DetalleActor.fromJSONMap(decodedData);
La ultima linea como la anterior pasa al siguiente metodo para mapearla:
DetalleActor.fromJSONMap(List<dynamic> jsonList) {
if (jsonList == null) return;
for (var item in jsonList) {
final persona = new Persona.fromJSONMap(item);
personas.add(persona);
}
}
Me lanza el siguiente error:
_TypeError (type '_InternalLinkedHashMap<String, dynamic>'
is not a subtype of type 'List<dynamic>')
Segun yo, es por que esta esperando algo como decodedData[xxx]
pero no se como resolverlo.
EDIT
CLASE/MODELO PERSONA
class DetalleActor{
List<Persona> personas=new List();
DetalleActor();
DetalleActor.fromJSONMap(List<dynamic> jsonList) {
if (jsonList == null) return;
for (var item in jsonList) {
final persona = new Persona.fromJSONMap(item);
personas.add(persona);
}
}
}
class Persona {
String birthday;
String knownForDepartment;
String deathday;
int id;
String name;
List<String> alsoKnownAs;
int gender;
String biography;
double popularity;
String placeOfBirth;
String profilePath;
String adult;
String imdbId;
String homepage;
Persona({
this.birthday,
this.knownForDepartment,
this.deathday,
this.id,
this.name,
this.alsoKnownAs,
this.gender,
this.biography,
this.popularity,
this.placeOfBirth,
this.profilePath,
this.adult,
this.imdbId,
this.homepage,
});
Persona.fromJSONMap(Map<String, dynamic> json){
birthday=json['birthday'];
knownForDepartment=json['known_for_department'];
deathday=json['death_day'];
id=json['id'];
name=json['name'];
alsoKnownAs=json['also_known_as'].cast<String>();
gender=json['gender'];
biography=json['biography'];
popularity=json['popularity'] / 1;
placeOfBirth=json['place_of_birth'];
profilePath=json['profile_path'];
adult=json['adult'];
imdbId=json['imdb_id'];
homepage=json['homepage'];
}
}
Persona
?