infinitewizard1010
infinitewizard1010

Reputation: 51

How do I parse this type of JSON for Flutter?

I am fairly new to Flutter and have been running against a roadblock with this task. I am trying to get the following JSON into my Flutter app. I tried using QuickType.io but it gives me a class with AAPL in it and I don't want to have to make a class for every single stock on the market. What is they best approach, maybe still with using Quicktype, for parsing this JSON?

{"AAPL":{"assetType":"EQUITY","assetMainType":"EQUITY","cusip":"037833100","symbol":"AAPL","description":"Apple Inc. - Common Stock","bidPrice":126.82,"bidSize":800,"bidId":"K","askPrice":126.9,"askSize":1000,"askId":"P","lastPrice":126.82,"lastSize":100,"lastId":"K","openPrice":128.78,"highPrice":130.2242,"lowPrice":127.0,"bidTick":" ","closePrice":128.91,"netChange":-2.09,"totalVolume":111598531,"quoteTimeInLong":1610758795615,"tradeTimeInLong":1610758795615,"mark":127.14,"exchange":"q","exchangeName":"NASD","marginable":true,"shortable":true,"volatility":0.0083,"digits":4,"52WkHigh":138.789,"52WkLow":53.1525,"nAV":0.0,"peRatio":40.0668,"divAmount":0.82,"divYield":0.64,"divDate":"2020-11-06 00:00:00.000","securityStatus":"Normal","regularMarketLastPrice":127.14,"regularMarketLastSize":90964,"regularMarketNetChange":-1.77,"regularMarketTradeTimeInLong":1610744400773,"netPercentChangeInDouble":-1.6213,"markChangeInDouble":-1.77,"markPercentChangeInDouble":-1.3731,"regularMarketPercentChangeInDouble":-1.3731,"delayed":true}}

Upvotes: 0

Views: 73

Answers (2)

Varun Saxena
Varun Saxena

Reputation: 1

Use Provider https://pub.dev/packages/provider , this will automatically update UI class everytime data changes

Upvotes: 0

Jitesh Mohite
Jitesh Mohite

Reputation: 34270

Please have a look at this video link which explain the easiest way to parse any json https://www.youtube.com/watch?v=FH4ckL37LHw

Parse Json:

class ClassName {
  AAPL aAPL;

  ClassName({this.aAPL});

  ClassName.fromJson(Map<String, dynamic> json) {
    aAPL = json['AAPL'] != null ? new AAPL.fromJson(json['AAPL']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.aAPL != null) {
      data['AAPL'] = this.aAPL.toJson();
    }
    return data;
  }
}

class AAPL {
  String assetType;
  String assetMainType;
  String cusip;
  String symbol;
  String description;
  double bidPrice;
  int bidSize;
  String bidId;
  double askPrice;
  int askSize;
  String askId;
  double lastPrice;
  int lastSize;
  String lastId;
  double openPrice;
  double highPrice;
  int lowPrice;
  String bidTick;
  double closePrice;
  double netChange;
  int totalVolume;
  int quoteTimeInLong;
  int tradeTimeInLong;
  double mark;
  String exchange;
  String exchangeName;
  bool marginable;
  bool shortable;
  double volatility;
  int digits;
  double d52WkHigh;
  double d52WkLow;
  int nAV;
  double peRatio;
  double divAmount;
  double divYield;
  String divDate;
  String securityStatus;
  double regularMarketLastPrice;
  int regularMarketLastSize;
  double regularMarketNetChange;
  int regularMarketTradeTimeInLong;
  double netPercentChangeInDouble;
  double markChangeInDouble;
  double markPercentChangeInDouble;
  double regularMarketPercentChangeInDouble;
  bool delayed;

  AAPL(
      {this.assetType,
      this.assetMainType,
      this.cusip,
      this.symbol,
      this.description,
      this.bidPrice,
      this.bidSize,
      this.bidId,
      this.askPrice,
      this.askSize,
      this.askId,
      this.lastPrice,
      this.lastSize,
      this.lastId,
      this.openPrice,
      this.highPrice,
      this.lowPrice,
      this.bidTick,
      this.closePrice,
      this.netChange,
      this.totalVolume,
      this.quoteTimeInLong,
      this.tradeTimeInLong,
      this.mark,
      this.exchange,
      this.exchangeName,
      this.marginable,
      this.shortable,
      this.volatility,
      this.digits,
      this.d52WkHigh,
      this.d52WkLow,
      this.nAV,
      this.peRatio,
      this.divAmount,
      this.divYield,
      this.divDate,
      this.securityStatus,
      this.regularMarketLastPrice,
      this.regularMarketLastSize,
      this.regularMarketNetChange,
      this.regularMarketTradeTimeInLong,
      this.netPercentChangeInDouble,
      this.markChangeInDouble,
      this.markPercentChangeInDouble,
      this.regularMarketPercentChangeInDouble,
      this.delayed});

  AAPL.fromJson(Map<String, dynamic> json) {
    assetType = json['assetType'];
    assetMainType = json['assetMainType'];
    cusip = json['cusip'];
    symbol = json['symbol'];
    description = json['description'];
    bidPrice = json['bidPrice'];
    bidSize = json['bidSize'];
    bidId = json['bidId'];
    askPrice = json['askPrice'];
    askSize = json['askSize'];
    askId = json['askId'];
    lastPrice = json['lastPrice'];
    lastSize = json['lastSize'];
    lastId = json['lastId'];
    openPrice = json['openPrice'];
    highPrice = json['highPrice'];
    lowPrice = json['lowPrice'];
    bidTick = json['bidTick'];
    closePrice = json['closePrice'];
    netChange = json['netChange'];
    totalVolume = json['totalVolume'];
    quoteTimeInLong = json['quoteTimeInLong'];
    tradeTimeInLong = json['tradeTimeInLong'];
    mark = json['mark'];
    exchange = json['exchange'];
    exchangeName = json['exchangeName'];
    marginable = json['marginable'];
    shortable = json['shortable'];
    volatility = json['volatility'];
    digits = json['digits'];
    d52WkHigh = json['52WkHigh'];
    d52WkLow = json['52WkLow'];
    nAV = json['nAV'];
    peRatio = json['peRatio'];
    divAmount = json['divAmount'];
    divYield = json['divYield'];
    divDate = json['divDate'];
    securityStatus = json['securityStatus'];
    regularMarketLastPrice = json['regularMarketLastPrice'];
    regularMarketLastSize = json['regularMarketLastSize'];
    regularMarketNetChange = json['regularMarketNetChange'];
    regularMarketTradeTimeInLong = json['regularMarketTradeTimeInLong'];
    netPercentChangeInDouble = json['netPercentChangeInDouble'];
    markChangeInDouble = json['markChangeInDouble'];
    markPercentChangeInDouble = json['markPercentChangeInDouble'];
    regularMarketPercentChangeInDouble =
        json['regularMarketPercentChangeInDouble'];
    delayed = json['delayed'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['assetType'] = this.assetType;
    data['assetMainType'] = this.assetMainType;
    data['cusip'] = this.cusip;
    data['symbol'] = this.symbol;
    data['description'] = this.description;
    data['bidPrice'] = this.bidPrice;
    data['bidSize'] = this.bidSize;
    data['bidId'] = this.bidId;
    data['askPrice'] = this.askPrice;
    data['askSize'] = this.askSize;
    data['askId'] = this.askId;
    data['lastPrice'] = this.lastPrice;
    data['lastSize'] = this.lastSize;
    data['lastId'] = this.lastId;
    data['openPrice'] = this.openPrice;
    data['highPrice'] = this.highPrice;
    data['lowPrice'] = this.lowPrice;
    data['bidTick'] = this.bidTick;
    data['closePrice'] = this.closePrice;
    data['netChange'] = this.netChange;
    data['totalVolume'] = this.totalVolume;
    data['quoteTimeInLong'] = this.quoteTimeInLong;
    data['tradeTimeInLong'] = this.tradeTimeInLong;
    data['mark'] = this.mark;
    data['exchange'] = this.exchange;
    data['exchangeName'] = this.exchangeName;
    data['marginable'] = this.marginable;
    data['shortable'] = this.shortable;
    data['volatility'] = this.volatility;
    data['digits'] = this.digits;
    data['52WkHigh'] = this.d52WkHigh;
    data['52WkLow'] = this.d52WkLow;
    data['nAV'] = this.nAV;
    data['peRatio'] = this.peRatio;
    data['divAmount'] = this.divAmount;
    data['divYield'] = this.divYield;
    data['divDate'] = this.divDate;
    data['securityStatus'] = this.securityStatus;
    data['regularMarketLastPrice'] = this.regularMarketLastPrice;
    data['regularMarketLastSize'] = this.regularMarketLastSize;
    data['regularMarketNetChange'] = this.regularMarketNetChange;
    data['regularMarketTradeTimeInLong'] = this.regularMarketTradeTimeInLong;
    data['netPercentChangeInDouble'] = this.netPercentChangeInDouble;
    data['markChangeInDouble'] = this.markChangeInDouble;
    data['markPercentChangeInDouble'] = this.markPercentChangeInDouble;
    data['regularMarketPercentChangeInDouble'] =
        this.regularMarketPercentChangeInDouble;
    data['delayed'] = this.delayed;
    return data;
  }
}

Upvotes: 1

Related Questions