在Flutter中发布解析数据

时间:2020-06-24 10:50:31

标签: json flutter parsing dart

我和我的代码似乎在这里崩溃。我无法使用jsonDecode解析此数据集,请帮忙。它应该很简单,但是只能通过jsonDecode

数据集

keepRevisionForever

失败代码

true

1 个答案:

答案 0 :(得分:0)

import 'dart:convert';

class Definitions {
  Definitions({this.data, this.id});
  List<DefData> data;
  String id;
  
  static Definitions fromJson(Map<String,dynamic> json) => Definitions(
    data: List<DefData>
      .from(json['data']
      .map((dynamic d ) => DefData.fromJson(d),)
      .toList(),),
    id: json['_id'],
  );
}

class DefData {
  DefData({this.name,this.visible});
  final String name;
  final bool visible;
  
  static DefData fromJson(Map<String,dynamic> json) => DefData(
    name: json['name'],
    visible: json['visible']
  );
}

void main() {
  
 const String _json = '[{"data":[{"name":"Alba","visible":true},  {"name":"Arad","visible":true}],"_id":"Counties"}]';
  
  List<Definitions> _foos = List.from(jsonDecode(_json))
    .map((dynamic d ) => Definitions.fromJson(d),)
    .toList();
  
  _foos.forEach((Definitions f) {
    f.data.forEach((Baz b) {
      print(b.name);
      print(b.visible);
    });
    print({f.id});
  });
}

如果您有类似的数据集,则可以使用它,它对我有用:)