在 Flutter 中反序列化 JSON 数据

Posted

技术标签:

【中文标题】在 Flutter 中反序列化 JSON 数据【英文标题】:Deserializing JSON data in Flutter 【发布时间】:2021-08-31 16:13:38 【问题描述】:

有问题的 JSON 数据是https://api.aladhan.com/v1/calendarByCity?city=London&country=United%20Kingdom&method=8&month=06&year=2021

List <PrayerTimesPerDay> prayerList; // Read the contents of nested List
// the bottom section is pretty inaccurate afaik



factory PrayerTimesPerDay.fromJson(Map<String, dynamic> json) 
    List timings = json["data"];
    return PrayerTimesPerDay(
      day: json['gregorian']['weekday']['en'],
      fajr: json['Fajr'],
      zuhr: json['Dhuhr'],
      asr: json['Asr'],
      maghrib: json['Maghrib'],
      isha: json['sha'],
    );
  

所以我的意图是将时间和日期读入一个名为 PrayerTimesPerDay 的类中,该类具有以下变量

final String day;
final String fajr;
  final String zuhr;
  final String asr;
  final String maghrib;
  final String isha;

我担心的是,我真的不明白如何将这种数据转换为这些变量,我设法在我的异步函数中将它们提取为 List 并能够从中打印一些数据,甚至计划制作一个for 循环(没有锻炼)将它们添加到列表中。 如果有解释将不胜感激

【问题讨论】:

你查看过JSON解析的官方文档吗? flutter.dev/docs/development/data-and-backend/json 我做了,但我不明白如何为这个特定的用例这样做,无论如何感谢这里的每个人的帮助,我会看看哪个有效:) 【参考方案1】:

这是一个完整的模型类,您可以阅读和理解。花了太多时间,但我很感激你的问题。

您只需复制并将其保存在单独的文件中,例如 model.dart

    // To parse this JSON data, do
//
//     final welcome = welcomeFromMap(jsonString);

import 'dart:convert';

class Welcome 
    Welcome(
        this.code,
        this.status,
        this.data,
    );

    final int code;
    final String status;
    final List<Datum> data;

    factory Welcome.fromJson(String str) => Welcome.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Welcome.fromMap(Map<String, dynamic> json) => Welcome(
        code: json["code"] == null ? null : json["code"],
        status: json["status"] == null ? null : json["status"],
        data: json["data"] == null ? null : List<Datum>.from(json["data"].map((x) => Datum.fromMap(x))),
    );

    Map<String, dynamic> toMap() => 
        "code": code == null ? null : code,
        "status": status == null ? null : status,
        "data": data == null ? null : List<dynamic>.from(data.map((x) => x.toMap())),
    ;


class Datum 
    Datum(
        this.timings,
        this.date,
        this.meta,
    );

    final Timings timings;
    final Date date;
    final Meta meta;

    factory Datum.fromJson(String str) => Datum.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Datum.fromMap(Map<String, dynamic> json) => Datum(
        timings: json["timings"] == null ? null : Timings.fromMap(json["timings"]),
        date: json["date"] == null ? null : Date.fromMap(json["date"]),
        meta: json["meta"] == null ? null : Meta.fromMap(json["meta"]),
    );

    Map<String, dynamic> toMap() => 
        "timings": timings == null ? null : timings.toMap(),
        "date": date == null ? null : date.toMap(),
        "meta": meta == null ? null : meta.toMap(),
    ;


class Date 
    Date(
        this.readable,
        this.timestamp,
        this.gregorian,
        this.hijri,
    );

    final String readable;
    final String timestamp;
    final Gregorian gregorian;
    final Hijri hijri;

    factory Date.fromJson(String str) => Date.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Date.fromMap(Map<String, dynamic> json) => Date(
        readable: json["readable"] == null ? null : json["readable"],
        timestamp: json["timestamp"] == null ? null : json["timestamp"],
        gregorian: json["gregorian"] == null ? null : Gregorian.fromMap(json["gregorian"]),
        hijri: json["hijri"] == null ? null : Hijri.fromMap(json["hijri"]),
    );

    Map<String, dynamic> toMap() => 
        "readable": readable == null ? null : readable,
        "timestamp": timestamp == null ? null : timestamp,
        "gregorian": gregorian == null ? null : gregorian.toMap(),
        "hijri": hijri == null ? null : hijri.toMap(),
    ;


class Gregorian 
    Gregorian(
        this.date,
        this.format,
        this.day,
        this.weekday,
        this.month,
        this.year,
        this.designation,
    );

    final String date;
    final Format format;
    final String day;
    final GregorianWeekday weekday;
    final GregorianMonth month;
    final String year;
    final Designation designation;

    factory Gregorian.fromJson(String str) => Gregorian.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Gregorian.fromMap(Map<String, dynamic> json) => Gregorian(
        date: json["date"] == null ? null : json["date"],
        format: json["format"] == null ? null : formatValues.map[json["format"]],
        day: json["day"] == null ? null : json["day"],
        weekday: json["weekday"] == null ? null : GregorianWeekday.fromMap(json["weekday"]),
        month: json["month"] == null ? null : GregorianMonth.fromMap(json["month"]),
        year: json["year"] == null ? null : json["year"],
        designation: json["designation"] == null ? null : Designation.fromMap(json["designation"]),
    );

    Map<String, dynamic> toMap() => 
        "date": date == null ? null : date,
        "format": format == null ? null : formatValues.reverse[format],
        "day": day == null ? null : day,
        "weekday": weekday == null ? null : weekday.toMap(),
        "month": month == null ? null : month.toMap(),
        "year": year == null ? null : year,
        "designation": designation == null ? null : designation.toMap(),
    ;


class Designation 
    Designation(
        this.abbreviated,
        this.expanded,
    );

    final Abbreviated abbreviated;
    final Expanded expanded;

    factory Designation.fromJson(String str) => Designation.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Designation.fromMap(Map<String, dynamic> json) => Designation(
        abbreviated: json["abbreviated"] == null ? null : abbreviatedValues.map[json["abbreviated"]],
        expanded: json["expanded"] == null ? null : expandedValues.map[json["expanded"]],
    );

    Map<String, dynamic> toMap() => 
        "abbreviated": abbreviated == null ? null : abbreviatedValues.reverse[abbreviated],
        "expanded": expanded == null ? null : expandedValues.reverse[expanded],
    ;


enum Abbreviated  AD, AH 

final abbreviatedValues = EnumValues(
    "AD": Abbreviated.AD,
    "AH": Abbreviated.AH
);

enum Expanded  ANNO_DOMINI, ANNO_HEGIRAE 

final expandedValues = EnumValues(
    "Anno Domini": Expanded.ANNO_DOMINI,
    "Anno Hegirae": Expanded.ANNO_HEGIRAE
);

enum Format  DD_MM_YYYY 

final formatValues = EnumValues(
    "DD-MM-YYYY": Format.DD_MM_YYYY
);

class GregorianMonth 
    GregorianMonth(
        this.number,
        this.en,
    );

    final int number;
    final PurpleEn en;

    factory GregorianMonth.fromJson(String str) => GregorianMonth.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory GregorianMonth.fromMap(Map<String, dynamic> json) => GregorianMonth(
        number: json["number"] == null ? null : json["number"],
        en: json["en"] == null ? null : purpleEnValues.map[json["en"]],
    );

    Map<String, dynamic> toMap() => 
        "number": number == null ? null : number,
        "en": en == null ? null : purpleEnValues.reverse[en],
    ;


enum PurpleEn  JUNE 

final purpleEnValues = EnumValues(
    "June": PurpleEn.JUNE
);

class GregorianWeekday 
    GregorianWeekday(
        this.en,
    );

    final String en;

    factory GregorianWeekday.fromJson(String str) => GregorianWeekday.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory GregorianWeekday.fromMap(Map<String, dynamic> json) => GregorianWeekday(
        en: json["en"] == null ? null : json["en"],
    );

    Map<String, dynamic> toMap() => 
        "en": en == null ? null : en,
    ;


class Hijri 
    Hijri(
        this.date,
        this.format,
        this.day,
        this.weekday,
        this.month,
        this.year,
        this.designation,
        this.holidays,
    );

    final String date;
    final Format format;
    final String day;
    final HijriWeekday weekday;
    final HijriMonth month;
    final String year;
    final Designation designation;
    final List<dynamic> holidays;

    factory Hijri.fromJson(String str) => Hijri.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Hijri.fromMap(Map<String, dynamic> json) => Hijri(
        date: json["date"] == null ? null : json["date"],
        format: json["format"] == null ? null : formatValues.map[json["format"]],
        day: json["day"] == null ? null : json["day"],
        weekday: json["weekday"] == null ? null : HijriWeekday.fromMap(json["weekday"]),
        month: json["month"] == null ? null : HijriMonth.fromMap(json["month"]),
        year: json["year"] == null ? null : json["year"],
        designation: json["designation"] == null ? null : Designation.fromMap(json["designation"]),
        holidays: json["holidays"] == null ? null : List<dynamic>.from(json["holidays"].map((x) => x)),
    );

    Map<String, dynamic> toMap() => 
        "date": date == null ? null : date,
        "format": format == null ? null : formatValues.reverse[format],
        "day": day == null ? null : day,
        "weekday": weekday == null ? null : weekday.toMap(),
        "month": month == null ? null : month.toMap(),
        "year": year == null ? null : year,
        "designation": designation == null ? null : designation.toMap(),
        "holidays": holidays == null ? null : List<dynamic>.from(holidays.map((x) => x)),
    ;


class HijriMonth 
    HijriMonth(
        this.number,
        this.en,
        this.ar,
    );

    final int number;
    final FluffyEn en;
    final Ar ar;

    factory HijriMonth.fromJson(String str) => HijriMonth.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory HijriMonth.fromMap(Map<String, dynamic> json) => HijriMonth(
        number: json["number"] == null ? null : json["number"],
        en: json["en"] == null ? null : fluffyEnValues.map[json["en"]],
        ar: json["ar"] == null ? null : arValues.map[json["ar"]],
    );

    Map<String, dynamic> toMap() => 
        "number": number == null ? null : number,
        "en": en == null ? null : fluffyEnValues.reverse[en],
        "ar": ar == null ? null : arValues.reverse[ar],
    ;


enum Ar  EMPTY, AR 

final arValues = EnumValues(
    "ذوالقعدة": Ar.AR,
    "شَوّال": Ar.EMPTY
);

enum FluffyEn  SHAWWL, DH_AL_QA_DAH 

final fluffyEnValues = EnumValues(
    "Dhū al-Qaʿdah": FluffyEn.DH_AL_QA_DAH,
    "Shawwāl": FluffyEn.SHAWWL
);

class HijriWeekday 
    HijriWeekday(
        this.en,
        this.ar,
    );

    final String en;
    final String ar;

    factory HijriWeekday.fromJson(String str) => HijriWeekday.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory HijriWeekday.fromMap(Map<String, dynamic> json) => HijriWeekday(
        en: json["en"] == null ? null : json["en"],
        ar: json["ar"] == null ? null : json["ar"],
    );

    Map<String, dynamic> toMap() => 
        "en": en == null ? null : en,
        "ar": ar == null ? null : ar,
    ;


class Meta 
    Meta(
        this.latitude,
        this.longitude,
        this.timezone,
        this.method,
        this.latitudeAdjustmentMethod,
        this.midnightMode,
        this.school,
        this.offset,
    );

    final double latitude;
    final double longitude;
    final Timezone timezone;
    final Method method;
    final LatitudeAdjustmentMethod latitudeAdjustmentMethod;
    final MidnightMode midnightMode;
    final MidnightMode school;
    final Map<String, int> offset;

    factory Meta.fromJson(String str) => Meta.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Meta.fromMap(Map<String, dynamic> json) => Meta(
        latitude: json["latitude"] == null ? null : json["latitude"].toDouble(),
        longitude: json["longitude"] == null ? null : json["longitude"].toDouble(),
        timezone: json["timezone"] == null ? null : timezoneValues.map[json["timezone"]],
        method: json["method"] == null ? null : Method.fromMap(json["method"]),
        latitudeAdjustmentMethod: json["latitudeAdjustmentMethod"] == null ? null : latitudeAdjustmentMethodValues.map[json["latitudeAdjustmentMethod"]],
        midnightMode: json["midnightMode"] == null ? null : midnightModeValues.map[json["midnightMode"]],
        school: json["school"] == null ? null : midnightModeValues.map[json["school"]],
        offset: json["offset"] == null ? null : Map.from(json["offset"]).map((k, v) => MapEntry<String, int>(k, v)),
    );

    Map<String, dynamic> toMap() => 
        "latitude": latitude == null ? null : latitude,
        "longitude": longitude == null ? null : longitude,
        "timezone": timezone == null ? null : timezoneValues.reverse[timezone],
        "method": method == null ? null : method.toMap(),
        "latitudeAdjustmentMethod": latitudeAdjustmentMethod == null ? null : latitudeAdjustmentMethodValues.reverse[latitudeAdjustmentMethod],
        "midnightMode": midnightMode == null ? null : midnightModeValues.reverse[midnightMode],
        "school": school == null ? null : midnightModeValues.reverse[school],
        "offset": offset == null ? null : Map.from(offset).map((k, v) => MapEntry<String, dynamic>(k, v)),
    ;


enum LatitudeAdjustmentMethod  ANGLE_BASED 

final latitudeAdjustmentMethodValues = EnumValues(
    "ANGLE_BASED": LatitudeAdjustmentMethod.ANGLE_BASED
);

class Method 
    Method(
        this.id,
        this.name,
        this.params,
    );

    final int id;
    final Name name;
    final Params params;

    factory Method.fromJson(String str) => Method.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Method.fromMap(Map<String, dynamic> json) => Method(
        id: json["id"] == null ? null : json["id"],
        name: json["name"] == null ? null : nameValues.map[json["name"]],
        params: json["params"] == null ? null : Params.fromMap(json["params"]),
    );

    Map<String, dynamic> toMap() => 
        "id": id == null ? null : id,
        "name": name == null ? null : nameValues.reverse[name],
        "params": params == null ? null : params.toMap(),
    ;


enum Name  GULF_REGION 

final nameValues = EnumValues(
    "Gulf Region": Name.GULF_REGION
);

class Params 
    Params(
        this.fajr,
        this.isha,
    );

    final double fajr;
    final Isha isha;

    factory Params.fromJson(String str) => Params.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Params.fromMap(Map<String, dynamic> json) => Params(
        fajr: json["Fajr"] == null ? null : json["Fajr"].toDouble(),
        isha: json["Isha"] == null ? null : ishaValues.map[json["Isha"]],
    );

    Map<String, dynamic> toMap() => 
        "Fajr": fajr == null ? null : fajr,
        "Isha": isha == null ? null : ishaValues.reverse[isha],
    ;


enum Isha  THE_90_MIN 

final ishaValues = EnumValues(
    "90 min": Isha.THE_90_MIN
);

enum MidnightMode  STANDARD 

final midnightModeValues = EnumValues(
    "STANDARD": MidnightMode.STANDARD
);

enum Timezone  EUROPE_LONDON 

final timezoneValues = EnumValues(
    "Europe/London": Timezone.EUROPE_LONDON
);

class Timings 
    Timings(
        this.fajr,
        this.sunrise,
        this.dhuhr,
        this.asr,
        this.sunset,
        this.maghrib,
        this.isha,
        this.imsak,
        this.midnight,
    );

    final Fajr fajr;
    final String sunrise;
    final String dhuhr;
    final String asr;
    final String sunset;
    final String maghrib;
    final String isha;
    final Imsak imsak;
    final String midnight;

    factory Timings.fromJson(String str) => Timings.fromMap(json.decode(str));

    String toJson() => json.encode(toMap());

    factory Timings.fromMap(Map<String, dynamic> json) => Timings(
        fajr: json["Fajr"] == null ? null : fajrValues.map[json["Fajr"]],
        sunrise: json["Sunrise"] == null ? null : json["Sunrise"],
        dhuhr: json["Dhuhr"] == null ? null : json["Dhuhr"],
        asr: json["Asr"] == null ? null : json["Asr"],
        sunset: json["Sunset"] == null ? null : json["Sunset"],
        maghrib: json["Maghrib"] == null ? null : json["Maghrib"],
        isha: json["Isha"] == null ? null : json["Isha"],
        imsak: json["Imsak"] == null ? null : imsakValues.map[json["Imsak"]],
        midnight: json["Midnight"] == null ? null : json["Midnight"],
    );

    Map<String, dynamic> toMap() => 
        "Fajr": fajr == null ? null : fajrValues.reverse[fajr],
        "Sunrise": sunrise == null ? null : sunrise,
        "Dhuhr": dhuhr == null ? null : dhuhr,
        "Asr": asr == null ? null : asr,
        "Sunset": sunset == null ? null : sunset,
        "Maghrib": maghrib == null ? null : maghrib,
        "Isha": isha == null ? null : isha,
        "Imsak": imsak == null ? null : imsakValues.reverse[imsak],
        "Midnight": midnight == null ? null : midnight,
    ;


enum Fajr  THE_0219_BST, THE_0218_BST, THE_0220_BST, THE_0221_BST, THE_0222_BST 

final fajrValues = EnumValues(
    "02:18 (BST)": Fajr.THE_0218_BST,
    "02:19 (BST)": Fajr.THE_0219_BST,
    "02:20 (BST)": Fajr.THE_0220_BST,
    "02:21 (BST)": Fajr.THE_0221_BST,
    "02:22 (BST)": Fajr.THE_0222_BST
);

enum Imsak  THE_0209_BST, THE_0208_BST, THE_0210_BST, THE_0211_BST, THE_0212_BST 

final imsakValues = EnumValues(
    "02:08 (BST)": Imsak.THE_0208_BST,
    "02:09 (BST)": Imsak.THE_0209_BST,
    "02:10 (BST)": Imsak.THE_0210_BST,
    "02:11 (BST)": Imsak.THE_0211_BST,
    "02:12 (BST)": Imsak.THE_0212_BST
);

class EnumValues<T> 
    Map<String, T> map;
    Map<T, String> reverseMap;

    EnumValues(this.map);

    Map<T, String> get reverse 
        if (reverseMap == null) 
            reverseMap = map.map((k, v) => new MapEntry(v, k));
        
        return reverseMap;
    

你只能使用那些需要的类。

【讨论】:

【参考方案2】:

你可以隐藏你的回复here

class PrayerTime 
  int code;
  String status;
  List<Data> data;

  PrayerTime(this.code, this.status, this.data);

  PrayerTime.fromJson(Map<String, dynamic> json) 
    code = json['code'];
    status = json['status'];
    if (json['data'] != null) 
      data = new List<Data>();
      json['data'].forEach((v) 
        data.add(new Data.fromJson(v));
      );
    
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['code'] = this.code;
    data['status'] = this.status;
    if (this.data != null) 
      data['data'] = this.data.map((v) => v.toJson()).toList();
    
    return data;
  


class Data 
  Timings timings;
  Date date;
  Meta meta;

  Data(this.timings, this.date, this.meta);

  Data.fromJson(Map<String, dynamic> json) 
    timings =
        json['timings'] != null ? new Timings.fromJson(json['timings']) : null;
    date = json['date'] != null ? new Date.fromJson(json['date']) : null;
    meta = json['meta'] != null ? new Meta.fromJson(json['meta']) : null;
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.timings != null) 
      data['timings'] = this.timings.toJson();
    
    if (this.date != null) 
      data['date'] = this.date.toJson();
    
    if (this.meta != null) 
      data['meta'] = this.meta.toJson();
    
    return data;
  


class Timings 
  String fajr;
  String sunrise;
  String dhuhr;
  String asr;
  String sunset;
  String maghrib;
  String isha;
  String imsak;
  String midnight;

  Timings(
      this.fajr,
      this.sunrise,
      this.dhuhr,
      this.asr,
      this.sunset,
      this.maghrib,
      this.isha,
      this.imsak,
      this.midnight);

  Timings.fromJson(Map<String, dynamic> json) 
    fajr = json['Fajr'];
    sunrise = json['Sunrise'];
    dhuhr = json['Dhuhr'];
    asr = json['Asr'];
    sunset = json['Sunset'];
    maghrib = json['Maghrib'];
    isha = json['Isha'];
    imsak = json['Imsak'];
    midnight = json['Midnight'];
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Fajr'] = this.fajr;
    data['Sunrise'] = this.sunrise;
    data['Dhuhr'] = this.dhuhr;
    data['Asr'] = this.asr;
    data['Sunset'] = this.sunset;
    data['Maghrib'] = this.maghrib;
    data['Isha'] = this.isha;
    data['Imsak'] = this.imsak;
    data['Midnight'] = this.midnight;
    return data;
  


class Date 
  String readable;
  String timestamp;
  Gregorian gregorian;
  Hijri hijri;

  Date(this.readable, this.timestamp, this.gregorian, this.hijri);

  Date.fromJson(Map<String, dynamic> json) 
    readable = json['readable'];
    timestamp = json['timestamp'];
    gregorian = json['gregorian'] != null
        ? new Gregorian.fromJson(json['gregorian'])
        : null;
    hijri = json['hijri'] != null ? new Hijri.fromJson(json['hijri']) : null;
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['readable'] = this.readable;
    data['timestamp'] = this.timestamp;
    if (this.gregorian != null) 
      data['gregorian'] = this.gregorian.toJson();
    
    if (this.hijri != null) 
      data['hijri'] = this.hijri.toJson();
    
    return data;
  


class Gregorian 
  String date;
  String format;
  String day;
  Weekday weekday;
  Month month;
  String year;
  Designation designation;

  Gregorian(
      this.date,
      this.format,
      this.day,
      this.weekday,
      this.month,
      this.year,
      this.designation);

  Gregorian.fromJson(Map<String, dynamic> json) 
    date = json['date'];
    format = json['format'];
    day = json['day'];
    weekday =
        json['weekday'] != null ? new Weekday.fromJson(json['weekday']) : null;
    month = json['month'] != null ? new Month.fromJson(json['month']) : null;
    year = json['year'];
    designation = json['designation'] != null
        ? new Designation.fromJson(json['designation'])
        : null;
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['date'] = this.date;
    data['format'] = this.format;
    data['day'] = this.day;
    if (this.weekday != null) 
      data['weekday'] = this.weekday.toJson();
    
    if (this.month != null) 
      data['month'] = this.month.toJson();
    
    data['year'] = this.year;
    if (this.designation != null) 
      data['designation'] = this.designation.toJson();
    
    return data;
  


class Weekday 
  String en;

  Weekday(this.en);

  Weekday.fromJson(Map<String, dynamic> json) 
    en = json['en'];
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['en'] = this.en;
    return data;
  


class Month 
  int number;
  String en;

  Month(this.number, this.en);

  Month.fromJson(Map<String, dynamic> json) 
    number = json['number'];
    en = json['en'];
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['number'] = this.number;
    data['en'] = this.en;
    return data;
  


class Designation 
  String abbreviated;
  String expanded;

  Designation(this.abbreviated, this.expanded);

  Designation.fromJson(Map<String, dynamic> json) 
    abbreviated = json['abbreviated'];
    expanded = json['expanded'];
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['abbreviated'] = this.abbreviated;
    data['expanded'] = this.expanded;
    return data;
  


class Hijri 
  String date;
  String format;
  String day;
  Weekday weekday;
  Month month;
  String year;
  Designation designation;
  List<Null> holidays;

  Hijri(
      this.date,
      this.format,
      this.day,
      this.weekday,
      this.month,
      this.year,
      this.designation,
      this.holidays);

  Hijri.fromJson(Map<String, dynamic> json) 
    date = json['date'];
    format = json['format'];
    day = json['day'];
    weekday =
        json['weekday'] != null ? new Weekday.fromJson(json['weekday']) : null;
    month = json['month'] != null ? new Month.fromJson(json['month']) : null;
    year = json['year'];
    designation = json['designation'] != null
        ? new Designation.fromJson(json['designation'])
        : null;
    if (json['holidays'] != null) 
      holidays = new List<Null>();
      json['holidays'].forEach((v) 
        holidays.add(new Null.fromJson(v));
      );
    
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['date'] = this.date;
    data['format'] = this.format;
    data['day'] = this.day;
    if (this.weekday != null) 
      data['weekday'] = this.weekday.toJson();
    
    if (this.month != null) 
      data['month'] = this.month.toJson();
    
    data['year'] = this.year;
    if (this.designation != null) 
      data['designation'] = this.designation.toJson();
    
    if (this.holidays != null) 
      data['holidays'] = this.holidays.map((v) => v.toJson()).toList();
    
    return data;
  


class Weekday 
  String en;
  String ar;

  Weekday(this.en, this.ar);

  Weekday.fromJson(Map<String, dynamic> json) 
    en = json['en'];
    ar = json['ar'];
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['en'] = this.en;
    data['ar'] = this.ar;
    return data;
  


class Month 
  int number;
  String en;
  String ar;

  Month(this.number, this.en, this.ar);

  Month.fromJson(Map<String, dynamic> json) 
    number = json['number'];
    en = json['en'];
    ar = json['ar'];
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['number'] = this.number;
    data['en'] = this.en;
    data['ar'] = this.ar;
    return data;
  


class Meta 
  double latitude;
  double longitude;
  String timezone;
  Method method;
  String latitudeAdjustmentMethod;
  String midnightMode;
  String school;
  Offset offset;

  Meta(
      this.latitude,
      this.longitude,
      this.timezone,
      this.method,
      this.latitudeAdjustmentMethod,
      this.midnightMode,
      this.school,
      this.offset);

  Meta.fromJson(Map<String, dynamic> json) 
    latitude = json['latitude'];
    longitude = json['longitude'];
    timezone = json['timezone'];
    method =
        json['method'] != null ? new Method.fromJson(json['method']) : null;
    latitudeAdjustmentMethod = json['latitudeAdjustmentMethod'];
    midnightMode = json['midnightMode'];
    school = json['school'];
    offset =
        json['offset'] != null ? new Offset.fromJson(json['offset']) : null;
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['latitude'] = this.latitude;
    data['longitude'] = this.longitude;
    data['timezone'] = this.timezone;
    if (this.method != null) 
      data['method'] = this.method.toJson();
    
    data['latitudeAdjustmentMethod'] = this.latitudeAdjustmentMethod;
    data['midnightMode'] = this.midnightMode;
    data['school'] = this.school;
    if (this.offset != null) 
      data['offset'] = this.offset.toJson();
    
    return data;
  


class Method 
  int id;
  String name;
  Params params;

  Method(this.id, this.name, this.params);

  Method.fromJson(Map<String, dynamic> json) 
    id = json['id'];
    name = json['name'];
    params =
        json['params'] != null ? new Params.fromJson(json['params']) : null;
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    if (this.params != null) 
      data['params'] = this.params.toJson();
    
    return data;
  


class Params 
  double fajr;
  String isha;

  Params(this.fajr, this.isha);

  Params.fromJson(Map<String, dynamic> json) 
    fajr = json['Fajr'];
    isha = json['Isha'];
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Fajr'] = this.fajr;
    data['Isha'] = this.isha;
    return data;
  


class Offset 
  int imsak;
  int fajr;
  int sunrise;
  int dhuhr;
  int asr;
  int maghrib;
  int sunset;
  int isha;
  int midnight;

  Offset(
      this.imsak,
      this.fajr,
      this.sunrise,
      this.dhuhr,
      this.asr,
      this.maghrib,
      this.sunset,
      this.isha,
      this.midnight);

  Offset.fromJson(Map<String, dynamic> json) 
    imsak = json['Imsak'];
    fajr = json['Fajr'];
    sunrise = json['Sunrise'];
    dhuhr = json['Dhuhr'];
    asr = json['Asr'];
    maghrib = json['Maghrib'];
    sunset = json['Sunset'];
    isha = json['Isha'];
    midnight = json['Midnight'];
  

  Map<String, dynamic> toJson() 
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Imsak'] = this.imsak;
    data['Fajr'] = this.fajr;
    data['Sunrise'] = this.sunrise;
    data['Dhuhr'] = this.dhuhr;
    data['Asr'] = this.asr;
    data['Maghrib'] = this.maghrib;
    data['Sunset'] = this.sunset;
    data['Isha'] = this.isha;
    data['Midnight'] = this.midnight;
    return data;
  

【讨论】:

【参考方案3】:

SalamoAlikom 兄弟。下面是我将如何去做。

factory PrayerTimesPerDay.fromJson(Map<String, dynamic> json) 

    var data = json["data"];
    var data_00 = data[0]; //<-- you could loop over here to get all the data!!!

    return PrayerTimesPerDay(
      day: data_00['date']['gregorian']['weekday']['en'],
      fajr: data_00[timings]['Fajr'],
      zuhr: data_00[timings]['Dhuhr'],
      asr: data_00[timings]['Asr'],
      maghrib: data_00[timings]['Maghrib'],
      isha: data_00[timings]['sha'],
    );
  

这可能有效,但我强烈推荐 official documentation 中提到的代码生成。这将需要您构建您的对象,以便它们可以轻松地从 JSON 转换为玩飞镖对象。

【讨论】:

以上是关于在 Flutter 中反序列化 JSON 数据的主要内容,如果未能解决你的问题,请参考以下文章

如何从实体框架中存在数据模型的json中反序列化对象?

在 Objective C 中反序列化 JSON 字符串

在 Visual Studio 中反序列化 JSON 文件时出错

在 FromForm 中反序列化 JSON 时的生命周期

为啥我不能在 Python 中反序列化带有单引号的 JSON 字符串?

jQuery/ASMX:在 C# 中反序列化 JSON 时出错