扑上JSON解析时出错。类型'_InternalLinkedHashMap <string,dynamic =“”>'不是类型转换中类型'ResultData'的子类型

时间:2019-01-14 12:19:27

标签: android json parsing dart flutter

我的json如下。

{  
   "result_data":{  
      "id":"b57457b0-4f73-11e8-92ae-01912016d38c",
      "rates":[  
         {  },
         {  }
      ]
   }
}

我想解析,下面是我解析的代码。 获取“ result_data”

class RootModel {
  ResultData resultData;

  RootModel(this.resultData);

  RootModel.fromJSON(Map<String, dynamic> response) {
    var list = response['result_data'] as ResultData;
    print(list.runtimeType);
  }
}

要解析结果数据对象。

class ResultData {
  String id;
  List<Rate> rates;
  ResultData(this.id, this.rates);

  ResultData.fromJSON(Map<String, dynamic> parsedJson) {
    this.id = parsedJson['id'] as String;
    this.rates = (parsedJson['rates'] as List)
        .map((json) => Rate.fromJSON(json))
        .toList();
  }
}

进一步费率模型还有其他模型和模型列表。 对于上述代码,我得到的错误是:

 E/flutter: [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
    type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'ResultData' in type cast
    #0      new RootModel.fromJSON (package:movies_streams/models/RootModel.dart:16:40)

最近三天我一直被困在这里,请帮忙。

3 个答案:

答案 0 :(得分:2)

您无法将地图强制转换为ResultData。

class RootModel {
  ResultData resultData;

  RootModel(this.resultData);

  RootModel.fromJSON(Map<String, dynamic> response) {
//    var list = response['result_data'] as ResultData;
    var list = ResultData.fromJSON(response['result_data']);
    print(list.runtimeType);
  }
}

答案 1 :(得分:1)

JSON反序列化没有任何奇迹。您必须自己调用fromJSON,而不仅仅是强制转换:

class RootModel {
  ResultData resultData;

  RootModel(this.resultData);

  RootModel.fromJSON(Map<String, dynamic> response)
    : resultData =  ResultData.fromJSON(response['result_data'] as Map<String, dynamic>);

}

答案 2 :(得分:0)

避免此类问题的一种可能方法是使用特殊工具创建数据模型。

例如:json_serializable或其他。

下面是一个示例,该示例显示了代码的外观,您必须用手编写代码,并且可能会出现一些错误。

// Generated by 'yaml2podo'
// Version: 0.1.11
// https://pub.dev/packages/yaml2podo

class ResultData {
  final List rates;

  ResultData({this.rates});

  factory ResultData.fromJson(Map map) {
    return ResultData(rates: _toList(map['rates'], (e) => e));
  }

  Map<String, dynamic> toJson() {
    var result = <String, dynamic>{};
    result['rates'] = _fromList(rates, (e) => e);
    return result;
  }
}

class RootModel {
  final ResultData resultData;

  RootModel({this.resultData});

  factory RootModel.fromJson(Map map) {
    return RootModel(
        resultData: _toObject(
            map['result_data'], (e) => ResultData.fromJson(e as Map)));
  }

  Map<String, dynamic> toJson() {
    var result = <String, dynamic>{};
    result['result_data'] = resultData?.toJson();
    return result;
  }
}

List _fromList(dynamic data, dynamic 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;
}

List<T> _toList<T>(dynamic 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;
}

T _toObject<T>(dynamic data, T Function(dynamic) fromJson) {
  if (data == null) {
    return null;
  }
  return fromJson(data);
}