json - 如何在Dart中为 map ,多层 map 创建模型

标签 json flutter dart

我有多层次的Json,我想为https://blockchain.info/ticker创建一个模型
这是我所做的工作无助于将其序列化为列表,就像我无法访问外部和内部Maps一样:

 final double delay;
 final double recentMarketPprice;
 final double buyPrice;
 final double sellPrice;
 final String symbol;

 BitRateValues(
      {this.delay,
 this.recentMarketPprice,
 this.buyPrice,
 this.sellPrice,
 this.symbol});

 factory BitRateValues.fromMap(Map<String, dynamic> data) {
 return BitRateValues(
        delay: data['15m'],
        recentMarketPprice: data['last'],
        buyPrice: data['buy'],
        sellPrice: data['sell'],
        symbol: data['symbol']);
  }
}

class BitRateMAp {
 Map<String, BitRateValues> bits;
 BitRateMAp({this.bits});
}```

please kinldy help  

最佳答案

欢迎来到SO。我建议下次将所有相关的代码和数据样本放入您的问题中。这样,如果API更改,URL损坏等,将来的读者仍然可以使用问题和答案。
所有这些货币都是JSON对象(“USD”,“AUD”等),而不是数组。因此,为了使用该JSON,必须将它们全部声明。
JSON:

{
  "USD" : {"15m" : 10956.55, "last" : 10956.55, "buy" : 10956.55, "sell" : 10956.55, "symbol" : "$"},
  "AUD" : {"15m" : 15023.62, "last" : 15023.62, "buy" : 15023.62, "sell" : 15023.62, "symbol" : "$"},
  "BRL" : {"15m" : 59143.45, "last" : 59143.45, "buy" : 59143.45, "sell" : 59143.45, "symbol" : "R$"},
  "CAD" : {"15m" : 14461.13, "last" : 14461.13, "buy" : 14461.13, "sell" : 14461.13, "symbol" : "$"},
  "CHF" : {"15m" : 9982.46, "last" : 9982.46, "buy" : 9982.46, "sell" : 9982.46, "symbol" : "CHF"},
  "CLP" : {"15m" : 8363136.86, "last" : 8363136.86, "buy" : 8363136.86, "sell" : 8363136.86, "symbol" : "$"},
  "CNY" : {"15m" : 74164.87, "last" : 74164.87, "buy" : 74164.87, "sell" : 74164.87, "symbol" : "¥"},
  "DKK" : {"15m" : 68828.23, "last" : 68828.23, "buy" : 68828.23, "sell" : 68828.23, "symbol" : "kr"},
  "EUR" : {"15m" : 9252.82, "last" : 9252.82, "buy" : 9252.82, "sell" : 9252.82, "symbol" : "€"},
  "GBP" : {"15m" : 8472.15, "last" : 8472.15, "buy" : 8472.15, "sell" : 8472.15, "symbol" : "£"},
  "HKD" : {"15m" : 84913.79, "last" : 84913.79, "buy" : 84913.79, "sell" : 84913.79, "symbol" : "$"},
  "INR" : {"15m" : 805690.37, "last" : 805690.37, "buy" : 805690.37, "sell" : 805690.37, "symbol" : "₹"},
  "ISK" : {"15m" : 1490967.05, "last" : 1490967.05, "buy" : 1490967.05, "sell" : 1490967.05, "symbol" : "kr"},
  "JPY" : {"15m" : 1145199.68, "last" : 1145199.68, "buy" : 1145199.68, "sell" : 1145199.68, "symbol" : "¥"},
  "KRW" : {"15m" : 1.276426884E7, "last" : 1.276426884E7, "buy" : 1.276426884E7, "sell" : 1.276426884E7, "symbol" : "₩"},
  "NZD" : {"15m" : 16212.46, "last" : 16212.46, "buy" : 16212.46, "sell" : 16212.46, "symbol" : "$"},
  "PLN" : {"15m" : 41210.73, "last" : 41210.73, "buy" : 41210.73, "sell" : 41210.73, "symbol" : "zł"},
  "RUB" : {"15m" : 828134.25, "last" : 828134.25, "buy" : 828134.25, "sell" : 828134.25, "symbol" : "RUB"},
  "SEK" : {"15m" : 96075.78, "last" : 96075.78, "buy" : 96075.78, "sell" : 96075.78, "symbol" : "kr"},
  "SGD" : {"15m" : 14887.32, "last" : 14887.32, "buy" : 14887.32, "sell" : 14887.32, "symbol" : "$"},
  "THB" : {"15m" : 340419.95, "last" : 340419.95, "buy" : 340419.95, "sell" : 340419.95, "symbol" : "฿"},
  "TRY" : {"15m" : 82859.99, "last" : 82859.99, "buy" : 82859.99, "sell" : 82859.99, "symbol" : "₺"},
  "TWD" : {"15m" : 317959.03, "last" : 317959.03, "buy" : 317959.03, "sell" : 317959.03, "symbol" : "NT$"}
}
镖:
class AllCurrencies {
  Currency uSD;
  Currency aUD;
  Currency bRL;
  Currency cAD;
  Currency cHF;
  Currency cLP;
  Currency cNY;
  Currency dKK;
  Currency eUR;
  Currency gBP;
  Currency hKD;
  Currency iNR;
  Currency iSK;
  Currency jPY;
  Currency kRW;
  Currency nZD;
  Currency pLN;
  Currency rUB;
  Currency sEK;
  Currency sGD;
  Currency tHB;
  Currency tRY;
  Currency tWD;

  AllCurrencies(
      {this.uSD,
        this.aUD,
        this.bRL,
        this.cAD,
        this.cHF,
        this.cLP,
        this.cNY,
        this.dKK,
        this.eUR,
        this.gBP,
        this.hKD,
        this.iNR,
        this.iSK,
        this.jPY,
        this.kRW,
        this.nZD,
        this.pLN,
        this.rUB,
        this.sEK,
        this.sGD,
        this.tHB,
        this.tRY,
        this.tWD});

  AllCurrencies.fromJson(Map<String, dynamic> json) {
    uSD = json['USD'] != null ? new Currency.fromJson(json['USD']) : null;
    aUD = json['AUD'] != null ? new Currency.fromJson(json['AUD']) : null;
    bRL = json['BRL'] != null ? new Currency.fromJson(json['BRL']) : null;
    cAD = json['CAD'] != null ? new Currency.fromJson(json['CAD']) : null;
    cHF = json['CHF'] != null ? new Currency.fromJson(json['CHF']) : null;
    cLP = json['CLP'] != null ? new Currency.fromJson(json['CLP']) : null;
    cNY = json['CNY'] != null ? new Currency.fromJson(json['CNY']) : null;
    dKK = json['DKK'] != null ? new Currency.fromJson(json['DKK']) : null;
    eUR = json['EUR'] != null ? new Currency.fromJson(json['EUR']) : null;
    gBP = json['GBP'] != null ? new Currency.fromJson(json['GBP']) : null;
    hKD = json['HKD'] != null ? new Currency.fromJson(json['HKD']) : null;
    iNR = json['INR'] != null ? new Currency.fromJson(json['INR']) : null;
    iSK = json['ISK'] != null ? new Currency.fromJson(json['ISK']) : null;
    jPY = json['JPY'] != null ? new Currency.fromJson(json['JPY']) : null;
    kRW = json['KRW'] != null ? new Currency.fromJson(json['KRW']) : null;
    nZD = json['NZD'] != null ? new Currency.fromJson(json['NZD']) : null;
    pLN = json['PLN'] != null ? new Currency.fromJson(json['PLN']) : null;
    rUB = json['RUB'] != null ? new Currency.fromJson(json['RUB']) : null;
    sEK = json['SEK'] != null ? new Currency.fromJson(json['SEK']) : null;
    sGD = json['SGD'] != null ? new Currency.fromJson(json['SGD']) : null;
    tHB = json['THB'] != null ? new Currency.fromJson(json['THB']) : null;
    tRY = json['TRY'] != null ? new Currency.fromJson(json['TRY']) : null;
    tWD = json['TWD'] != null ? new Currency.fromJson(json['TWD']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.uSD != null) {
      data['USD'] = this.uSD.toJson();
    }
    if (this.aUD != null) {
      data['AUD'] = this.aUD.toJson();
    }
    if (this.bRL != null) {
      data['BRL'] = this.bRL.toJson();
    }
    if (this.cAD != null) {
      data['CAD'] = this.cAD.toJson();
    }
    if (this.cHF != null) {
      data['CHF'] = this.cHF.toJson();
    }
    if (this.cLP != null) {
      data['CLP'] = this.cLP.toJson();
    }
    if (this.cNY != null) {
      data['CNY'] = this.cNY.toJson();
    }
    if (this.dKK != null) {
      data['DKK'] = this.dKK.toJson();
    }
    if (this.eUR != null) {
      data['EUR'] = this.eUR.toJson();
    }
    if (this.gBP != null) {
      data['GBP'] = this.gBP.toJson();
    }
    if (this.hKD != null) {
      data['HKD'] = this.hKD.toJson();
    }
    if (this.iNR != null) {
      data['INR'] = this.iNR.toJson();
    }
    if (this.iSK != null) {
      data['ISK'] = this.iSK.toJson();
    }
    if (this.jPY != null) {
      data['JPY'] = this.jPY.toJson();
    }
    if (this.kRW != null) {
      data['KRW'] = this.kRW.toJson();
    }
    if (this.nZD != null) {
      data['NZD'] = this.nZD.toJson();
    }
    if (this.pLN != null) {
      data['PLN'] = this.pLN.toJson();
    }
    if (this.rUB != null) {
      data['RUB'] = this.rUB.toJson();
    }
    if (this.sEK != null) {
      data['SEK'] = this.sEK.toJson();
    }
    if (this.sGD != null) {
      data['SGD'] = this.sGD.toJson();
    }
    if (this.tHB != null) {
      data['THB'] = this.tHB.toJson();
    }
    if (this.tRY != null) {
      data['TRY'] = this.tRY.toJson();
    }
    if (this.tWD != null) {
      data['TWD'] = this.tWD.toJson();
    }
    return data;
  }
}

class Currency {
  double d15m;
  double last;
  double buy;
  double sell;
  String symbol;

  Currency({this.d15m, this.last, this.buy, this.sell, this.symbol});

  Currency.fromJson(Map<String, dynamic> json) {
    d15m = json['15m'];
    last = json['last'];
    buy = json['buy'];
    sell = json['sell'];
    symbol = json['symbol'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['15m'] = this.d15m;
    data['last'] = this.last;
    data['buy'] = this.buy;
    data['sell'] = this.sell;
    data['symbol'] = this.symbol;
    return data;
  }
}

关于json - 如何在Dart中为 map ,多层 map 创建模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63983244/

相关文章:

json - 使用 postgresql 将 xml 转换为 json

flutter - 没有用于TabBarView抖动的TabController

flutter - 将变量传递给 Stateful 类之外的 Widget

android - 如何将 json 字符串解码为 UTF-8?

flutter - 水平 ListView 上的最小高度

java - 客户端渲染 JSON 与 GPB( Protocol Buffer )

ruby-on-rails - 使用 activerecord 在 jsonb postgres 中查询嵌套的哈希数组是否存在 key

flutter - 'List<dynamic>'类型不是 'FutureOr<String>'类型的子类型

php - JSON 的正确 MIME 类型?

flutter - 我在摄像头屏幕选项卡上时想隐藏应用程序栏和底部导航栏