我有以下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"
},
]
我有以下方法来获取JSON并将其传递给模型:
final respuesta=await http.get(url);
final decodedData=json.decode(respuesta.body);
final cast=new Cast.fromJSONMap(decodedData['cast']);
最后一行是在模型中引用此方法的位置,从此处映射并保存数据:
Cast.fromJSONMap(List<dynamic> jsonList) {
if (jsonList == null) return;
for (var item in jsonList) {
final actor = new Actor.fromJSONMap(item);
actores.add(actor);
}
}
一切对我都很好
现在我有以下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
}
我想恢复它并像上一个一样映射它,但是此JSON没有像上一个示例那样包装它的xxx: []
,因此,当使用相同方法将其传递给它进行映射时:
final respuesta=await http.get(url);
final decodedData=json.decode(respuesta.body);
final person=new DetalleActor.fromJSONMap(decodedData);
与上一行类似,最后一行转到以下方法进行映射:
DetalleActor.fromJSONMap(List<dynamic> jsonList) {
if (jsonList == null) return;
for (var item in jsonList) {
final persona = new Persona.fromJSONMap(item);
personas.add(persona);
}
}
它显示了以下错误:
_TypeError (type '_InternalLinkedHashMap<String, dynamic>'
is not a subtype of type 'List<dynamic>')
根据我的说法,这是因为他正在等待类似decodedData[xxx]
之类的东西,但是我不知道该如何解决。
答案 0 :(得分:0)
根据我的理解,Person
是also_know_as
字段内的元素,那么您遇到的错误是因为DetailActor.fromJSONMap(List <dynamic> jsonList)
在等待List
时等待Map
或dynamic
。
这应该可以解决您的问题:
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'].map((val) => val).toList();
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'];
}
}
用法
final respuesta=await http.get(url);
final decodedData=json.decode(respuesta.body);
final person= Persona.fromJSONMap(decodedData);
答案 1 :(得分:0)
另一种安全的方法:
class DetalleActor {
final bool adult;
final List<String> alsoKnownAs;
final String biography;
final String birthday;
final Object deathday;
final int gender;
final Object homepage;
final int id;
final String imdbId;
final String knownForDepartment;
final String name;
final String placeOfBirth;
final double popularity;
final String profilePath;
DetalleActor(
{this.adult,
this.alsoKnownAs,
this.biography,
this.birthday,
this.deathday,
this.gender,
this.homepage,
this.id,
this.imdbId,
this.knownForDepartment,
this.name,
this.placeOfBirth,
this.popularity,
this.profilePath});
factory DetalleActor.fromJson(Map<String, dynamic> json) {
return DetalleActor(
adult: json['adult'] as bool,
alsoKnownAs: _toList(json['also_known_as'], (e) => e as String),
biography: json['biography'] as String,
birthday: json['birthday'] as String,
deathday: json['deathday'],
gender: json['gender'] as int,
homepage: json['homepage'],
id: json['id'] as int,
imdbId: json['imdb_id'] as String,
knownForDepartment: json['known_for_department'] as String,
name: json['name'] as String,
placeOfBirth: json['place_of_birth'] as String,
popularity: _toDouble(json['popularity']),
profilePath: json['profile_path'] as String,
);
}
Map<String, dynamic> toJson() {
return {
'adult': adult,
'also_known_as': _fromList(alsoKnownAs, (e) => e),
'biography': biography,
'birthday': birthday,
'deathday': deathday,
'gender': gender,
'homepage': homepage,
'id': id,
'imdb_id': imdbId,
'known_for_department': knownForDepartment,
'name': name,
'place_of_birth': placeOfBirth,
'popularity': popularity,
'profile_path': profilePath,
};
}
}
List _fromList(data, Function(dynamic) toJson) {
if (data == null) {
return null;
}
var result = [];
for (var element in data) {
var value;
if (element != null) {
value = toJson(element);
}
result.add(value);
}
return result;
}
double _toDouble(data) {
if (data == null) {
return null;
}
if (data is int) {
return data.toDouble();
}
return data as double;
}
List<T> _toList<T>(data, T Function(dynamic) fromJson) {
if (data == null) {
return null;
}
var result = <T>[];
for (var element in data) {
T value;
if (element != null) {
value = fromJson(element);
}
result.add(value);
}
return result;
}
/*
DetalleActor:
"birthday": String
"known_for_department": String
"deathday": Object
"id": int
"name": String
"also_known_as": List<String>
"gender": int
"biography": String
"popularity": double
"place_of_birth": String
"profile_path": String
"adult": bool
"imdb_id": String
"homepage": Object
*/