如何在json响应中处理地图和列表?

时间:2019-08-06 11:31:26

标签: flutter dart

x.g.dart是基于波纹管类ApplicableFlight生成的。我要实现的是,如果json文件中的FlightSegmentReference是map而不是list,我希望它创建一个列表并将该map添加到list中并返回它。 目前,它仅以这种方式处理FlightSegmentReference的列表。我想保持ApplicableFlight类不变,但是如果地图出现,我想创建一个列表并添加地图。

x.g.dart

ApplicableFlight _$ApplicableFlightFromJson(Map json) {
  return $checkedNew('ApplicableFlight', json, () {
    $checkKeys(json, allowedKeys: const [
      'FlightReferences',
      'FlightSegmentReference',
      'OriginDestinationReferences'
    ]);
    final val = ApplicableFlight(
      $checkedConvert(json, 'FlightReferences', (v) => v),
      $checkedConvert(
          json,
          'FlightSegmentReference',
          (v) => (v as List)
              ?.map((e) => e == null
                  ? null
                  : FlightSegmentReference.fromJson((e as Map)?.map(
                      (k, e) => MapEntry(k as String, e),
                    )))
              ?.toList()),
      $checkedConvert(json, 'OriginDestinationReferences', (v) => v),
    );
    return val;
  }, fieldKeyMap: const {
    'flightReference': 'FlightReferences',
    'flightSegmentReference': 'FlightSegmentReference',
    'originDestinationReferences': 'OriginDestinationReferences'
  });
}

这是

@JsonSerializable()
class ApplicableFlight {
  @JsonKey(name: "FlightReferences")
  var flightReference;
  @JsonKey(name: "FlightSegmentReference")
  List<FlightSegmentReference> flightSegmentReference;
  @JsonKey(name: "OriginDestinationReferences")
  var originDestinationReferences;

  ApplicableFlight(this.flightReference, this.flightSegmentReference,
      this.originDestinationReferences);

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

0 个答案:

没有答案