DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
kamal bunkar
kamal bunkar

Posted on

How to Convert JSON into Dart Class

After a lot of Research I find a Amazing Tool which can save my 40 Minutes to convert JSON into Dart class manually. Here is one Online Tool JSON to Dart Convert which can convert json response into dart class on button click. It is really very simple to use.
I am Getting JSON response from the Server. The JSON response is very complex. I want to access the value of "min" and "max" inside the "RIFUSD" List. Below is the JSON response which I am getting from server.


{
  "RIFUSD": [
    {
      "timestamp": "2021-02-13T16:00:00.000Z",
      "open": "0.3257370",
      "close": "0.3257370",
      "min": "0.3257370",
      "max": "0.3257370",
      "volume": "49",
      "volumeQuote": "15.9611130"
    },
    {
      "timestamp": "2021-02-13T12:00:00.000Z",
      "open": "0.3015120",
      "close": "0.3216128",
      "min": "0.3015120",
      "max": "0.3216768",
      "volume": "4079",
      "volumeQuote": "1298.0319504"
    }
  ],
  "BERRYUSD": [
    {
      "timestamp": "2021-02-13T04:00:00.000Z",
      "open": "0.00061800",
      "close": "0.00061780",
      "min": "0.00061000",
      "max": "0.00071783",
      "volume": "10460",
      "volumeQuote": "6.89477840"
    },
    {
      "timestamp": "2021-02-12T20:00:00.000Z",
      "open": "0.00060489",
      "close": "0.00061800",
      "min": "0.00048829",
      "max": "0.00061800",
      "volume": "466690",
      "volumeQuote": "228.12405820"
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

It looks very hard to get the value of "min" from this JSON. So using the tool JSON to Dart I got the Dart file on button click. It save my all most 40 Minutes ...
Here is Generated Dart File


class Model_Candle {
  List<RIFUSD> rIFUSD;
  List<BERRYUSD> bERRYUSD;

  Model_Candle({this.rIFUSD, this.bERRYUSD});

  Model_Candle.fromJson(Map<String, dynamic> json) {
    if (json['RIFUSD'] != null) {
      rIFUSD = new List<RIFUSD>();
      json['RIFUSD'].forEach((v) {
        rIFUSD.add(new RIFUSD.fromJson(v));
      });
    }
    if (json['BERRYUSD'] != null) {
      bERRYUSD = new List<BERRYUSD>();
      json['BERRYUSD'].forEach((v) {
        bERRYUSD.add(new BERRYUSD.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.rIFUSD != null) {
      data['RIFUSD'] = this.rIFUSD.map((v) => v.toJson()).toList();
    }
    if (this.bERRYUSD != null) {
      data['BERRYUSD'] = this.bERRYUSD.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class RIFUSD {
  String timestamp;
  String open;
  String close;
  String min;
  String max;
  String volume;
  String volumeQuote;

  RIFUSD(
      {this.timestamp,
      this.open,
      this.close,
      this.min,
      this.max,
      this.volume,
      this.volumeQuote});

  RIFUSD.fromJson(Map<String, dynamic> json) {
    timestamp = json['timestamp'];
    open = json['open'];
    close = json['close'];
    min = json['min'];
    max = json['max'];
    volume = json['volume'];
    volumeQuote = json['volumeQuote'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['timestamp'] = this.timestamp;
    data['open'] = this.open;
    data['close'] = this.close;
    data['min'] = this.min;
    data['max'] = this.max;
    data['volume'] = this.volume;
    data['volumeQuote'] = this.volumeQuote;
    return data;
  }
}

Enter fullscreen mode Exit fullscreen mode

If you need my Help on Native App Development, Flutter App Development, OR Website Development. Please feel Free to contact me www.DripCoding.com

Top comments (0)

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.