簡體   English   中英

如何為map的map創建Model,Dart中的多級882477763218788

[英]How to create a Model for a map of map , multilevel map in Dart

我有一個多級 Json,我想創建一個 model 用於在https://blockchain.info/ticker中找到

這是我所做的,但無助於將其序列化為列表,就像我無法訪問外部和內部地圖一樣:

 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$"}
}

DART:

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;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM