简体   繁体   English

用 flutter dart 解析大 json

[英]parse big json with flutter dart

the json code below must be parsed into an object, the object is very complex I have to extrapolate only the repr_file_name field with the jpg file, how can I do to parse this json quickly?下面的json代码必须解析成一个对象,对象非常复杂我只需要用jpg文件推断repr_file_name字段,我该如何快速解析这个json? it's a json array and i have to use dart to parse it, does anyone have any ideas?它是一个 json 数组,我必须使用 dart 来解析它,有没有人有任何想法? this array includes a series of fields but the only one I need is the jp file,这个数组包含一系列字段,但我唯一需要的是 jp 文件,

Json:杰森:

[
    {
        "info_id": 488,
        "app_id": 35,
        "info_title": "01 ",
        "icat_code": "23",
        "info_lat": 0,
        "info_lon": 0,
        "info_date_from": "2017-08-01",
        "info_date_to": "2030-12-31",
        "info_ts_ins": "2017-08-01T14:32:49.538639+02:00",
        "res": [
            {
                "res_id": 1313,
                "res_desc": "Titolo",
                "app_id": 35,
                "res_tag": "Titolo",
                "info_id": 488,
                "repr": [
                    {
                        "repr_id": 1794,
                        "res_id": 1313,
                        "repr_mime_type": "text/plain",
                        "repr_lang": "en",
                        "repr_devices": "*",
                        "repr_text": "sdsd"
                    },
                    {
                        "repr_id": 1793,
                        "res_id": 1313,
                        "repr_mime_type": "text/plain",
                        "repr_lang": "it",
                        "repr_devices": "*",
                        "repr_text": "sdsd"
                    }
                ]
            },
            {
                "res_id": 1314,
                "res_desc": "Descrizione",
                "app_id": 35,
                "res_tag": "Descrizione",
                "info_id": 488,
                "repr": [
                    {
                        "repr_id": 1792,
                        "res_id": 1314,
                        "repr_mime_type": "text/plain",
                        "repr_lang": "en",
                        "repr_devices": "*",
                        "repr_text": "my text."
                    },
                    {
                        "repr_id": 1791,
                        "res_id": 1314,
                        "repr_mime_type": "text/plain",
                        "repr_lang": "it",
                        "repr_devices": "*",
                        "repr_text": "my text"
                    }
                ]
            },
            {
                "res_id": 1315,
                "res_desc": "Immagine",
                "app_id": 35,
                "res_tag": "Immagine",
                "info_id": 488,
                "repr": [
                    {
                        "repr_id": 1817,
                        "res_id": 1315,
                        "repr_mime_type": "image/jpeg",
                        "repr_lang": "**",
                        "repr_devices": "*",
                        "repr_file_name": "wheelchair_new.jpg"
                    }
                ]
            }
        ]
    },
...

]

I have tried converting this JSON to dart using this .我曾尝试使用this将此 JSON 转换为 dart。 It is recommended to use such conversions as it's a good code practise建议使用此类转换,因为这是一种很好的代码实践

class SampleClass {
  int infoId;
  int appId;
  String infoTitle;
  String icatCode;
  int infoLat;
  int infoLon;
  String infoDateFrom;
  String infoDateTo;
  String infoTsIns;
  List<Res> res;

  SampleClass(
      {this.infoId,
      this.appId,
      this.infoTitle,
      this.icatCode,
      this.infoLat,
      this.infoLon,
      this.infoDateFrom,
      this.infoDateTo,
      this.infoTsIns,
      this.res});

  SampleClass.fromJson(Map<String, dynamic> json) {
    infoId = json['info_id'];
    appId = json['app_id'];
    infoTitle = json['info_title'];
    icatCode = json['icat_code'];
    infoLat = json['info_lat'];
    infoLon = json['info_lon'];
    infoDateFrom = json['info_date_from'];
    infoDateTo = json['info_date_to'];
    infoTsIns = json['info_ts_ins'];
    if (json['res'] != null) {
      res = new List<Res>();
      json['res'].forEach((v) {
        res.add(new Res.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['info_id'] = this.infoId;
    data['app_id'] = this.appId;
    data['info_title'] = this.infoTitle;
    data['icat_code'] = this.icatCode;
    data['info_lat'] = this.infoLat;
    data['info_lon'] = this.infoLon;
    data['info_date_from'] = this.infoDateFrom;
    data['info_date_to'] = this.infoDateTo;
    data['info_ts_ins'] = this.infoTsIns;
    if (this.res != null) {
      data['res'] = this.res.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Res {
  int resId;
  String resDesc;
  int appId;
  String resTag;
  int infoId;
  List<Repr> repr;

  Res(
      {this.resId,
      this.resDesc,
      this.appId,
      this.resTag,
      this.infoId,
      this.repr});

  Res.fromJson(Map<String, dynamic> json) {
    resId = json['res_id'];
    resDesc = json['res_desc'];
    appId = json['app_id'];
    resTag = json['res_tag'];
    infoId = json['info_id'];
    if (json['repr'] != null) {
      repr = new List<Repr>();
      json['repr'].forEach((v) {
        repr.add(new Repr.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['res_id'] = this.resId;
    data['res_desc'] = this.resDesc;
    data['app_id'] = this.appId;
    data['res_tag'] = this.resTag;
    data['info_id'] = this.infoId;
    if (this.repr != null) {
      data['repr'] = this.repr.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Repr {
  int reprId;
  int resId;
  String reprMimeType;
  String reprLang;
  String reprDevices;
  String reprText;
  String reprFileName;

  Repr(
      {this.reprId,
      this.resId,
      this.reprMimeType,
      this.reprLang,
      this.reprDevices,
      this.reprText,
      this.reprFileName});

  Repr.fromJson(Map<String, dynamic> json) {
    reprId = json['repr_id'];
    resId = json['res_id'];
    reprMimeType = json['repr_mime_type'];
    reprLang = json['repr_lang'];
    reprDevices = json['repr_devices'];
    reprText = json['repr_text'];
    reprFileName = json['repr_file_name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['repr_id'] = this.reprId;
    data['res_id'] = this.resId;
    data['repr_mime_type'] = this.reprMimeType;
    data['repr_lang'] = this.reprLang;
    data['repr_devices'] = this.reprDevices;
    data['repr_text'] = this.reprText;
    data['repr_file_name'] = this.reprFileName;
    return data;
  }
}
  1. if you are using the Flutter < 2.0 (Null safety) then use this online parser https://javiercbk.github.io/json_to_dart/如果您使用的是 Flutter < 2.0(空安全),请使用此在线解析器https://javiercbk.github.io/json_to_dart/

  2. otherwise use this online parse Flutter > 2.0 https://app.quicktype.io/否则使用此在线解析 Flutter > 2.0 https://app.quicktype.io/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM