在flutter中使用json_serializable对JSON对象进行反序列化

时间:2018-06-22 17:54:14

标签: json dart flutter

我已经在json注释的帮助下使用json_serializable包生成了代码。我建立了另一个模型类,用于将json类的特定属性映射到模型类。但是,当反序列化对象时,出现以下错误 type int不是字符串的子类型 根据以上错误,我假设反序列化不会产生列表,而是会产生映射。我将模型类转换为列表,但没有帮助。我对飞镖不熟悉,因此对这一主题的理解有限。

这是代码。

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 个答案:

答案 0 :(得分:1)

我不能百分百确定-您应该在问题中包含来自API的示例JSON响应-但我认为它位于您的Condition对象中。你有

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

但是我认为应该是

final int code;

您所描述的错误基本上意味着您的代码正在尝试将值解码为String,但是找到了int,因此无法对其进行解码。

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