首页 文章

在flutter中使用json_serializable反序列化json对象

提问于
浏览
1

我在json注释的帮助下使用json_serializable包生成了代码 . 我已经构建了另一个模型类,用于将json类的特定属性映射到模型类 . 但是,当反序列化对象时,我得到以下错误 type int is not a subtype of string 从上面的错误我假设反序列化没有帮助't yield a list but a map. I converted the model class to list but it didn' t . 我对这个问题的了解有限,因为我是dart的新手 .

这是代码 .

import 'package:json_annotation/json_annotation.dart';

part 'json.g.dart';

@JsonSerializable()
class BaseClass extends Object with _$BaseClassSerializerMixin {
@JsonKey(name: "location")
final Location location;
@JsonKey(name: "current")
final Current current;


BaseClass({this.location, this.current});

factory BaseClass.fromJson(Map<String, dynamic> json) =>
  _$BaseClassFromJson(json);
}

@JsonSerializable()
class Location extends Object with _$LocationSerializerMixin {
final String name;
final String region;
final String country;
final double lon;
final double lat;
@JsonKey(name: "tz_id")
final String timezone;
@JsonKey(name: "localtime")
final String localtime;

Location({
 this.country,
 this.lat,
 this.localtime,
 this.lon,
 this.name,
 this.region,
 this.timezone,
 });

factory Location.fromJson(Map<String, dynamic> json) =>
  _$LocationFromJson(json);
}

@JsonSerializable()
class Current extends Object with _$CurrentSerializerMixin {
@JsonKey(name: "wind_mph")
final double windmph;
@JsonKey(name: "wind_kph")
final double windkph;
@JsonKey(name: "wind_dir")
final String windDirection;
@JsonKey(name: "wind_degree")
final double winddegree;
final int cloud;
@JsonKey(name: "pressure_mb")
final double pressuremb;
@JsonKey(name: "pressure_in")
final double pressurein;
@JsonKey(name: "precip_mm")
final double precipmm;
@JsonKey(name: "precip_in")
final double precipin;
final int humidity;
@JsonKey(name: "feelslike_c")
final double centrigade;
@JsonKey(name: "feelslike_f")
final double faranheit;
@JsonKey(name: "temp_c")
final double tempc;
@JsonKey(name: "temp_f")
final double tempf;
@JsonKey(name: "vis_km")
final double visionKM;
@JsonKey(name: "vis_miles")
final double visionM;

final Condition condition;

Current({
this.centrigade,
this.cloud,
this.faranheit,
this.humidity,
this.pressuremb,
this.tempc,
this.precipmm,
this.precipin,
this.pressurein,
this.tempf,
this.winddegree,
this.windkph,
this.windmph,
this.windDirection,
this.condition,
this.visionKM,
this.visionM,
});

factory Current.fromJson(Map<String, dynamic> json) =>
  _$CurrentFromJson(json);
}

@JsonSerializable() 
class Condition extends Object with _$ConditionSerializerMixin {
final String text;
final String icon;
final String code;

Condition({this.text, this.code, this.icon});

factory Condition.fromJson(Map<String, dynamic> json) =>
  _$ConditionFromJson(json);
}

天气模型

WeatherModel.fromResponse(BaseClass base)
  : centrigade = base.current.centrigade,
    tempc = base.current.tempc,
    tempf = base.current.tempf,
    faranheit = base.current.faranheit,
    cloud = base.current.cloud,
    humidity = base.current.humidity,
    winddegree = base.current.winddegree,
    windkph = base.current.windkph,
    windmph = base.current.windmph,
    precipin = base.current.precipin,
    precipmm = base.current.precipmm,
    pressuremb = base.current.pressuremb,
    pressurein = base.current.precipin,
    description = base.current.condition.text,
    icon = base.current.condition.icon;

反序列化代码

Future<List<WeatherModel>> getWeatherData(String city) async {
Uri uri = new Uri.https(
  "api.apixu.com", "v1/forecast.json", {"key": key, "q": city});
print(uri);
final response = await http.get(uri);
final responseJson = json.decode(response.body);
print(responseJson);
var data = BaseClass.fromJson(responseJson);
print(data);
return WeatherModel.fromResponse(data) as List; 
//Tried (BaseClass.fromResponse(responseJson) as List).map((i => 
WeatherModel.fromResponse(i)).toList();
}

1 回答

  • 1

    我无法100%确定 - 你应该在你的问题中包含来自API的示例JSON响应 - 但我认为它在你的Condition对象中 . 你有

    final String text;
    final String icon;
    final String code;
    

    但我认为应该如此

    final int code;
    

    您所描述的错误基本上意味着您的代码尝试将值解码为String,但发现 int 因此无法对其进行解码 .

    此外,您可能希望将wind_degree从Double更改为Int,至少根据apixu文档 .

相关问题