如何为Flutter解析这种类型的JSON?
我是一个新手,在完成这项任务时遇到了障碍。我正在尝试将以下JSON添加到我的Flatter应用程序中。我尝试使用QuickType.io,但它给了我一个包含AAPL的类,我不想为市场上的每只股票都创建一个类。他们解析这个JSON的最佳方法是什么,可能仍然使用Quicktype如何为Flutter解析这种类型的JSON?,json,flutter,dart,Json,Flutter,Dart,我是一个新手,在完成这项任务时遇到了障碍。我正在尝试将以下JSON添加到我的Flatter应用程序中。我尝试使用QuickType.io,但它给了我一个包含AAPL的类,我不想为市场上的每只股票都创建一个类。他们解析这个JSON的最佳方法是什么,可能仍然使用Quicktype {"AAPL":{"assetType":"EQUITY","assetMainType":"EQUITY","
{"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}}
请看一下这个视频链接,它解释了解析任何json的最简单方法 解析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;
}
}
类名{
AAPL-AAPL;
类名({this.aAPL});
fromJson(映射json){
aAPL=json['aAPL']!=null?新的aAPL.fromJson(json['aAPL']):null;
}
映射到JSON(){
最终地图数据=新地图();
如果(this.aAPL!=null){
data['AAPL']=this.AAPL.toJson();
}
返回数据;
}
}
AAPL类{
字符串资产类型;
字符串assetMainType;
弦尖;
字符串符号;
字符串描述;
双倍标价;
int-bidSize;
字符串bidId;
双askPrice;
int askSize;
串阿斯基德;
双倍价格;
int lastSize;
字符串lastId;
双倍开盘价;
双高价格;
国际低价;
串珠;
双倍收盘价;
双网变;
总体积;
int quoteTimeInLong;
国际贸易时间长;
双标记;
字符串交换;
字符串交换名;
布尔边缘化;
布尔短路;
双重波动;
整数位数;
双D52WK高;
双d52WkLow;
国际导航;
双手术;
双倍金额;
双倍产量;
字符串divDate;
字符串安全状态;
双倍正常市场价格;
国际标准市场的最后规模;
双重调节变化;
国际定期市场交易时间长;
双净百分比变化双倍;
双标记双标记;
双标记;双标记;
双调节器;
布尔延迟;
AAPL(
{this.assetType,
此.assetMainType,
这个,库希普,
这个符号,
这个.说明,,
这个价格,
这个尺寸,
这是比迪德先生,
这个,阿斯克帕斯,
这个,askSize,
这个,阿斯基德,
这个价格,
这是最后的尺寸,
这是拉斯蒂德,
这是公开价格,
这个,高价,
这个,低价,
这是比迪克,
这个价格,
这个.netChange,
这个,总音量,
这是我的名字,
这个,交易时间很长,
这个,马克,
这个,交换,,
这是exchangeName,
这是一个可边缘化的,
这个,短的,
这是波动性,
这个数字,
这个.d52wk高,
这是d52WkLow,
这个,导航,
这是我的作业,
这个金额,,
这个,这个产量,,
这个日期,
这是安全状态,
这是正规市场的最新价格,
这是我的最新尺码,
这是一个经常性的市场变化,
这是正常的市场交易时间,
这个.netPercentChangeInDouble,
这是一个很好的例子,
这是一个非常复杂的问题,
这是一个有规律的市场变化,
这是延迟的});
AAPL.fromJson(映射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'];
biddick=json['biddick'];
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'];
波动率=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'];
定期市场百分比变化双倍=
json['regularMarketPercentChangeInDouble'];
delayed=json['delayed'];
}
映射到JSON(){
最终地图数据=新地图();
数据['assetType']=this.assetType;
数据['assetMainType']=this.assetMainType;
数据['cusip']=this.cusip;
数据['symbol']=this.symbol;
数据['description']=this.description;
数据['bidPrice']=此.bidPrice;
数据['bidSize']=this.bidSize;
数据['bidId']=this.bidId;
数据['askPrice']=this.askPrice;
数据['askSize']=this.askSize;
数据['askId']=this.askId;
数据['lastPrice']=this.lastPrice;
数据['lastSize']=this.lastSize;
数据['lastId']=this.lastId;
数据['openPrice']=this.openPrice;
数据['highPrice']=这个.highPrice;
数据['lowPrice']=this.lowPrice;
数据['biddick']=this.biddick;
数据['closePrice']=this.closePrice;
数据['netChange']=this.netChange;
数据['totalVolume']=此.totalVolume;
数据['quoteTimeInLong']=this.quoteTimeInLong;
数据['tradeTimeInLong']=this.tradeTimeInLong;
数据['mark']=this.mark;
数据['exchange']=this.exchange;
数据['exchangeName']=this.exchangeName;