从 TD Ameritrade 反序列化 JSON
Deserializing JSON from TD Ameritrade
我正在从 TD Ameritrade 的 API 中提取报价数据,它正在生成以下 JSON:
{
"AAPL": {
"52WkHigh": 145.09,
"52WkLow": 53.1525,
"askId": "P",
"askPrice": 121.08,
"askSize": 1700,
"assetMainType": "EQUITY",
"assetType": "EQUITY",
"bidId": "P",
"bidPrice": 121.06,
"bidSize": 600,
"bidTick": " ",
"closePrice": 121.03,
"cusip": "037833100",
"delayed": "false",
"description": "Apple Inc. - Common Stock",
"digits": 4,
"divAmount": 0.82,
"divDate": "2021-02-05 00:00:00.000",
"divYield": 0.68,
"exchange": "q",
"exchangeName": "NASD",
"highPrice": 121.17,
"lastId": "D",
"lastPrice": 121.08,
"lastSize": 200,
"lowPrice": 119.16,
"marginable": "true",
"mark": 121.03,
"markChangeInDouble": 0.0,
"markPercentChangeInDouble": 0.0,
"nAV": 0.0,
"netChange": 0.05,
"netPercentChangeInDouble": 0.0413,
"openPrice": 120.4,
"peRatio": 32.9702,
"quoteTimeInLong": 1615597198023,
"regularMarketLastPrice": 121.03,
"regularMarketLastSize": 7,
"regularMarketNetChange": 0.0,
"regularMarketPercentChangeInDouble": 0.0,
"regularMarketTradeTimeInLong": 1615582801729,
"securityStatus": "Normal",
"shortable": "true",
"symbol": "AAPL",
"totalVolume": 88105050,
"tradeTimeInLong": 1615597198932,
"volatility": 0.0085
}
}
Class 从 JSON 创建:
public class Quote
{
public float _52WkHigh { get; set; }
public float _52WkLow { get; set; }
public string askId { get; set; }
public float askPrice { get; set; }
public int askSize { get; set; }
public string assetMainType { get; set; }
public string assetType { get; set; }
public string bidId { get; set; }
public float bidPrice { get; set; }
public int bidSize { get; set; }
public string bidTick { get; set; }
public float closePrice { get; set; }
public string cusip { get; set; }
public string delayed { get; set; }
public string description { get; set; }
public int digits { get; set; }
public float divAmount { get; set; }
public string divDate { get; set; }
public float divYield { get; set; }
public string exchange { get; set; }
public string exchangeName { get; set; }
public float highPrice { get; set; }
public string lastId { get; set; }
public float lastPrice { get; set; }
public int lastSize { get; set; }
public float lowPrice { get; set; }
public string marginable { get; set; }
public float mark { get; set; }
public float markChangeInDouble { get; set; }
public float markPercentChangeInDouble { get; set; }
public float nAV { get; set; }
public float netChange { get; set; }
public float netPercentChangeInDouble { get; set; }
public float openPrice { get; set; }
public float peRatio { get; set; }
public long quoteTimeInLong { get; set; }
public float regularMarketLastPrice { get; set; }
public int regularMarketLastSize { get; set; }
public float regularMarketNetChange { get; set; }
public float regularMarketPercentChangeInDouble { get; set; }
public long regularMarketTradeTimeInLong { get; set; }
public string securityStatus { get; set; }
public string shortable { get; set; }
public string symbol { get; set; }
public int totalVolume { get; set; }
public long tradeTimeInLong { get; set; }
public float volatility { get; set; }
}
我正在调用 Python 脚本来获取 JSON 数据,然后将其重定向到我的 C# 应用程序。
但是,当我反序列化它时,没有数据填充:
public void GetQuotes(string symbol)
{
var errors = "";
var results = "";
this.psi.Arguments = $"\"{this.script}\" -q {symbol}";
Quote quote;
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
results = process.StandardOutput.ReadToEnd();
Debug.Print(results);
quote = JsonConvert.DeserializeObject<Quote>(results);
}
MessageBox.Show(quote.symbol);
}
我已经能够反序列化 Json 并取得了相对一致的成功,但我不明白为什么我要从这个 JSON.
中加载空白对象
您需要创建一个包含报价单对象的根对象。
public class RootObject
{
public Quote AAPL { get;set;}
}
并反序列化为这个,
var rootObject = JsonConvert.DeserializeObject<RootObject>(results);
Quote quote = rootObject.AAPL;
当您反序列化为 Quote 时,您会得到空值,因为原始 json 中没有与任何 Quote 属性匹配的 属性。由于你的 JSON 包含 1 属性 (AAPL) 并且它的值是 Quote
类型,你必须反序列化为 RootObject.
根据您对变量键的看法,您应该反序列化为 Dictionary<string, Quote>
。
var obj = JsonConvert.DeserializeObject<Dictionary<string, Quote>>(results);
Quote quote = obj.FirstOrDefault().Value;
您也可以列出所有键并使用 foreach 循环,但如果您知道永远只有一个键,FirstOrDefault()
也可以。
感谢 Jawad 的快速提示。因为 'AAPL' 是一个不断变化的键,所以我不得不将 JSON 反序列化为 Dictionary<string, Quote>
来填充数据:
public void GetQuotes(string symbol)
{
var errors = "";
var results = "";
this.psi.Arguments = $"\"{this.script}\" -q {symbol}";
Dictionary<string, Quote> quotes;
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
results = process.StandardOutput.ReadToEnd();
Debug.Print(results);
quotes = JsonConvert.DeserializeObject<Dictionary<string, Quote>>(results);
}
}
我正在从 TD Ameritrade 的 API 中提取报价数据,它正在生成以下 JSON:
{
"AAPL": {
"52WkHigh": 145.09,
"52WkLow": 53.1525,
"askId": "P",
"askPrice": 121.08,
"askSize": 1700,
"assetMainType": "EQUITY",
"assetType": "EQUITY",
"bidId": "P",
"bidPrice": 121.06,
"bidSize": 600,
"bidTick": " ",
"closePrice": 121.03,
"cusip": "037833100",
"delayed": "false",
"description": "Apple Inc. - Common Stock",
"digits": 4,
"divAmount": 0.82,
"divDate": "2021-02-05 00:00:00.000",
"divYield": 0.68,
"exchange": "q",
"exchangeName": "NASD",
"highPrice": 121.17,
"lastId": "D",
"lastPrice": 121.08,
"lastSize": 200,
"lowPrice": 119.16,
"marginable": "true",
"mark": 121.03,
"markChangeInDouble": 0.0,
"markPercentChangeInDouble": 0.0,
"nAV": 0.0,
"netChange": 0.05,
"netPercentChangeInDouble": 0.0413,
"openPrice": 120.4,
"peRatio": 32.9702,
"quoteTimeInLong": 1615597198023,
"regularMarketLastPrice": 121.03,
"regularMarketLastSize": 7,
"regularMarketNetChange": 0.0,
"regularMarketPercentChangeInDouble": 0.0,
"regularMarketTradeTimeInLong": 1615582801729,
"securityStatus": "Normal",
"shortable": "true",
"symbol": "AAPL",
"totalVolume": 88105050,
"tradeTimeInLong": 1615597198932,
"volatility": 0.0085
}
}
Class 从 JSON 创建:
public class Quote
{
public float _52WkHigh { get; set; }
public float _52WkLow { get; set; }
public string askId { get; set; }
public float askPrice { get; set; }
public int askSize { get; set; }
public string assetMainType { get; set; }
public string assetType { get; set; }
public string bidId { get; set; }
public float bidPrice { get; set; }
public int bidSize { get; set; }
public string bidTick { get; set; }
public float closePrice { get; set; }
public string cusip { get; set; }
public string delayed { get; set; }
public string description { get; set; }
public int digits { get; set; }
public float divAmount { get; set; }
public string divDate { get; set; }
public float divYield { get; set; }
public string exchange { get; set; }
public string exchangeName { get; set; }
public float highPrice { get; set; }
public string lastId { get; set; }
public float lastPrice { get; set; }
public int lastSize { get; set; }
public float lowPrice { get; set; }
public string marginable { get; set; }
public float mark { get; set; }
public float markChangeInDouble { get; set; }
public float markPercentChangeInDouble { get; set; }
public float nAV { get; set; }
public float netChange { get; set; }
public float netPercentChangeInDouble { get; set; }
public float openPrice { get; set; }
public float peRatio { get; set; }
public long quoteTimeInLong { get; set; }
public float regularMarketLastPrice { get; set; }
public int regularMarketLastSize { get; set; }
public float regularMarketNetChange { get; set; }
public float regularMarketPercentChangeInDouble { get; set; }
public long regularMarketTradeTimeInLong { get; set; }
public string securityStatus { get; set; }
public string shortable { get; set; }
public string symbol { get; set; }
public int totalVolume { get; set; }
public long tradeTimeInLong { get; set; }
public float volatility { get; set; }
}
我正在调用 Python 脚本来获取 JSON 数据,然后将其重定向到我的 C# 应用程序。 但是,当我反序列化它时,没有数据填充:
public void GetQuotes(string symbol)
{
var errors = "";
var results = "";
this.psi.Arguments = $"\"{this.script}\" -q {symbol}";
Quote quote;
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
results = process.StandardOutput.ReadToEnd();
Debug.Print(results);
quote = JsonConvert.DeserializeObject<Quote>(results);
}
MessageBox.Show(quote.symbol);
}
我已经能够反序列化 Json 并取得了相对一致的成功,但我不明白为什么我要从这个 JSON.
中加载空白对象您需要创建一个包含报价单对象的根对象。
public class RootObject
{
public Quote AAPL { get;set;}
}
并反序列化为这个,
var rootObject = JsonConvert.DeserializeObject<RootObject>(results);
Quote quote = rootObject.AAPL;
当您反序列化为 Quote 时,您会得到空值,因为原始 json 中没有与任何 Quote 属性匹配的 属性。由于你的 JSON 包含 1 属性 (AAPL) 并且它的值是 Quote
类型,你必须反序列化为 RootObject.
根据您对变量键的看法,您应该反序列化为 Dictionary<string, Quote>
。
var obj = JsonConvert.DeserializeObject<Dictionary<string, Quote>>(results);
Quote quote = obj.FirstOrDefault().Value;
您也可以列出所有键并使用 foreach 循环,但如果您知道永远只有一个键,FirstOrDefault()
也可以。
感谢 Jawad 的快速提示。因为 'AAPL' 是一个不断变化的键,所以我不得不将 JSON 反序列化为 Dictionary<string, Quote>
来填充数据:
public void GetQuotes(string symbol)
{
var errors = "";
var results = "";
this.psi.Arguments = $"\"{this.script}\" -q {symbol}";
Dictionary<string, Quote> quotes;
using (var process = Process.Start(psi))
{
errors = process.StandardError.ReadToEnd();
results = process.StandardOutput.ReadToEnd();
Debug.Print(results);
quotes = JsonConvert.DeserializeObject<Dictionary<string, Quote>>(results);
}
}