嵌套JSON解析混乱

时间:2019-09-28 20:22:24

标签: flutter

解析JSON抖动时出错

I/flutter ( 6858): type 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'List<SearchModel>'

我在哪里做错了?如何解析错误的确切文件/行位置?

{
  "total": 24,
  "totalPages": 1,
  "currentPage": 1,
  "perPageRecords": 50,
  "values": [
    {
      "_id": "563e2cce31821525104dfe0c",
      "name": "dard ho dil mein to dawaa kiji",
      "deleted": false
    },
    {
      "_id": "563e2cce31821525104dfe0b",
      "name": "mulk-e-adam main yaaruun ky Id ho rahi hai",
      "description": "",
      "deleted": false
    }
]

样本模型采用这种方式

class SearchPagination {
  int total;
  int totalPages;
  String currentPage;
  int perPageRecords;
  List<SearchModel> values;

  SearchPagination({this.total, this.totalPages, this.currentPage, this.perPageRecords, this.values});

  factory SearchPagination.fromJson(Map<String, dynamic> json) {
    return SearchPagination(
      total: json["total"] as int,
      totalPages: json["totalPages"] as int,
      currentPage: json["currentPage"] as int,
      perPageRecords: json["perPageRecords"] as int,
      values: json["values"].map((a) => SearchModel.fromJson(a)),
    );
  }
}

class SearchModel {
  final String id;
  final String name;
  final String description;
  final bool deleted;

  SearchModel({this.id, this.name, this.description, this.deleted});

  factory SearchModel.fromJson(Map<SearchModel, dynamic> json) {
    return SearchModel(id: json["id"], name: json["name"], description: json["description"] as String, deleted: json["deleted"]);
  }
}

通过这种方式调用服务

  Future<SearchPagination> fetchSearches({int page: 1}) async {
    var url = Uri.https(baseFinalUrl, 'additional/searches', {'page': page.toString()});
    return _getJson(url).then((json) {
      print(json);

      return SearchPagination.fromJson(json);
    });
  }

解析JSON时的确切错误是什么?线到线调试器是否有抖动?

2 个答案:

答案 0 :(得分:1)

替换

      values: json["values"].map((a) => SearchModel.fromJson(a)),

使用

      values: (json["values"] as List).map((a) => SearchModel.fromJson(a)).toList(),

由于map的结果不是列表,因此必须将其转换为List

答案 1 :(得分:1)

通过访问here

创建您的飞镖类

将您的json数据粘贴到那里,它将输出所需的类文件

使用该类文件获取您的json数据