C# 反序列化 Json 对象以使用动态 属性 名称列出
C# Deserializing Json objec to list with dynamic property name
这是我正在使用的json:
{
"UEPS": {
"assetType": "EQUITY",
"assetMainType": "EQUITY",
"cusip": "64107N206",
"symbol": "UEPS",
"description": "Net 1 UEPS Technologies, Inc. - Common Stock",
"bidPrice": 4.2,
"bidSize": 100,
"bidId": "Q",
"askPrice": 5.75,
"askSize": 200,
"askId": "K",
"lastPrice": 4.71,
"lastSize": 700,
"lastId": "Q",
"openPrice": 4.59,
"highPrice": 4.77,
"lowPrice": 4.59,
"bidTick": " ",
"closePrice": 4.71,
"netChange": 0,
"totalVolume": 33908,
"quoteTimeInLong": 1643410807235,
"tradeTimeInLong": 1643403601408,
"mark": 4.71,
"exchange": "q",
"exchangeName": "NASD",
"marginable": true,
"shortable": true,
"volatility": 0.3684,
"digits": 4,
"52WkHigh": 6.97,
"52WkLow": 3.84,
"nAV": 0,
"peRatio": 0,
"divAmount": 0,
"divYield": 0,
"divDate": "",
"securityStatus": "Normal",
"regularMarketLastPrice": 4.71,
"regularMarketLastSize": 7,
"regularMarketNetChange": 0,
"regularMarketTradeTimeInLong": 1643403601408,
"netPercentChangeInDouble": 0,
"markChangeInDouble": 0,
"markPercentChangeInDouble": 0,
"regularMarketPercentChangeInDouble": 0,
"delayed": false,
"realtimeEntitled": true
},
"SRT": {
"assetType": "EQUITY",
"assetMainType": "EQUITY",
"cusip": "85569C107",
"symbol": "SRT",
"description": "StarTek, Inc. Common Stock",
"bidPrice": 2.77,
"bidSize": 100,
"bidId": "T",
"askPrice": 5.39,
"askSize": 0,
"askId": " ",
"lastPrice": 5.07,
"lastSize": 2000,
"lastId": "N",
"openPrice": 5,
"highPrice": 5.09,
"lowPrice": 4.915,
"bidTick": " ",
"closePrice": 5.07,
"netChange": 0,
"totalVolume": 54200,
"quoteTimeInLong": 1643418000039,
"tradeTimeInLong": 1643414400004,
"mark": 5.07,
"exchange": "n",
"exchangeName": "NYSE",
"marginable": true,
"shortable": true,
"volatility": 0.2976,
"digits": 2,
"52WkHigh": 9.8,
"52WkLow": 3.8,
"nAV": 0,
"peRatio": 0,
"divAmount": 0,
"divYield": 0,
"divDate": "",
"securityStatus": "Normal",
"regularMarketLastPrice": 5.07,
"regularMarketLastSize": 20,
"regularMarketNetChange": 0,
"regularMarketTradeTimeInLong": 1643414400004,
"netPercentChangeInDouble": 0,
"markChangeInDouble": 0,
"markPercentChangeInDouble": 0,
"regularMarketPercentChangeInDouble": 0,
"delayed": false,
"realtimeEntitled": true
},
"PTPI": {
"assetType": "EQUITY",
"assetMainType": "EQUITY",
"cusip": "71678J100",
"symbol": "PTPI",
"description": "Petros Pharmaceuticals, Inc. - Common Stock",
"bidPrice": 1.59,
"bidSize": 500,
"bidId": "P",
"askPrice": 1.62,
"askSize": 400,
"askId": "P",
"lastPrice": 1.6199,
"lastSize": 0,
"lastId": "D",
"openPrice": 1.56,
"highPrice": 1.6693,
"lowPrice": 1.45,
"bidTick": " ",
"closePrice": 1.62,
"netChange": -0.0001,
"totalVolume": 922374,
"quoteTimeInLong": 1643417960464,
"tradeTimeInLong": 1643417960466,
"mark": 1.62,
"exchange": "q",
"exchangeName": "NASD",
"marginable": false,
"shortable": false,
"volatility": 0.2206,
"digits": 4,
"52WkHigh": 5.2,
"52WkLow": 1.19,
"nAV": 0,
"peRatio": 0,
"divAmount": 0,
"divYield": 0,
"divDate": "",
"securityStatus": "Normal",
"regularMarketLastPrice": 1.62,
"regularMarketLastSize": 12,
"regularMarketNetChange": 0,
"regularMarketTradeTimeInLong": 1643403600790,
"netPercentChangeInDouble": -0.0062,
"markChangeInDouble": 0,
"markPercentChangeInDouble": 0,
"regularMarketPercentChangeInDouble": 0,
"delayed": false,
"realtimeEntitled": true
}
}
这里是 class 我正在尝试反序列化
public class Qoute
{
public Dictionary<string, Equity> Equity { get; set; }
}
public class Equity
{
public string assetType { get; set; }
public string assetMainType { get; set; }
public string cusip { get; set; }
public string symbol { get; set; }
public string description { get; set; }
public float bidPrice { get; set; }
public int bidSize { get; set; }
public string bidId { get; set; }
public float askPrice { get; set; }
public int askSize { get; set; }
public string askId { get; set; }
public float lastPrice { get; set; }
public int lastSize { get; set; }
public string lastId { get; set; }
public float openPrice { get; set; }
public float highPrice { get; set; }
public float lowPrice { get; set; }
public string bidTick { get; set; }
public float closePrice { get; set; }
public float netChange { get; set; }
public int totalVolume { get; set; }
public long quoteTimeInLong { get; set; }
public long tradeTimeInLong { get; set; }
public float mark { get; set; }
public string exchange { get; set; }
public string exchangeName { get; set; }
public bool marginable { get; set; }
public bool shortable { get; set; }
public float volatility { get; set; }
public int digits { get; set; }
public float _52WkHigh { get; set; }
public float _52WkLow { get; set; }
public float nAV { get; set; }
public float peRatio { get; set; }
public float divAmount { get; set; }
public float divYield { get; set; }
public string divDate { get; set; }
public string securityStatus { get; set; }
public float regularMarketLastPrice { get; set; }
public int regularMarketLastSize { get; set; }
public float regularMarketNetChange { get; set; }
public long regularMarketTradeTimeInLong { get; set; }
public float netPercentChangeInDouble { get; set; }
public float markChangeInDouble { get; set; }
public float markPercentChangeInDouble { get; set; }
public float regularMarketPercentChangeInDouble { get; set; }
public bool delayed { get; set; }
public bool realtimeEntitled { get; set; }
}
}
当我尝试转换为我的 class 的 List<> 时,我收到以下响应:
var x= JsonConvert.DeserializeObject<List<Model.Qoutes.Qoute>>(response);
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List
1[TDAPI.Model.Qoutes.Qoute]' 因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。
要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的`
当我尝试只获取一个对象时,Class Equity
为空:
var x2 = JsonConvert.DeserializeObject<Model.Qoutes.Qoute>(response);
我的理解是 public Dictionary<string, Equity> Equity { get; set; }
应该处理动态 属性 名称。我是否漏掉了一些明显的东西?
假设 json 有一个对象作为根,而不是数组,并且该对象没有 Equity
属性,就像您的 Quote
模型一样,它具有包含要映射到 Equity
的对象的属性,因此请尝试:
var result = JsonConvert.DeserializeObject<Dictionary<string, Equity>>(response);
这是我正在使用的json:
{
"UEPS": {
"assetType": "EQUITY",
"assetMainType": "EQUITY",
"cusip": "64107N206",
"symbol": "UEPS",
"description": "Net 1 UEPS Technologies, Inc. - Common Stock",
"bidPrice": 4.2,
"bidSize": 100,
"bidId": "Q",
"askPrice": 5.75,
"askSize": 200,
"askId": "K",
"lastPrice": 4.71,
"lastSize": 700,
"lastId": "Q",
"openPrice": 4.59,
"highPrice": 4.77,
"lowPrice": 4.59,
"bidTick": " ",
"closePrice": 4.71,
"netChange": 0,
"totalVolume": 33908,
"quoteTimeInLong": 1643410807235,
"tradeTimeInLong": 1643403601408,
"mark": 4.71,
"exchange": "q",
"exchangeName": "NASD",
"marginable": true,
"shortable": true,
"volatility": 0.3684,
"digits": 4,
"52WkHigh": 6.97,
"52WkLow": 3.84,
"nAV": 0,
"peRatio": 0,
"divAmount": 0,
"divYield": 0,
"divDate": "",
"securityStatus": "Normal",
"regularMarketLastPrice": 4.71,
"regularMarketLastSize": 7,
"regularMarketNetChange": 0,
"regularMarketTradeTimeInLong": 1643403601408,
"netPercentChangeInDouble": 0,
"markChangeInDouble": 0,
"markPercentChangeInDouble": 0,
"regularMarketPercentChangeInDouble": 0,
"delayed": false,
"realtimeEntitled": true
},
"SRT": {
"assetType": "EQUITY",
"assetMainType": "EQUITY",
"cusip": "85569C107",
"symbol": "SRT",
"description": "StarTek, Inc. Common Stock",
"bidPrice": 2.77,
"bidSize": 100,
"bidId": "T",
"askPrice": 5.39,
"askSize": 0,
"askId": " ",
"lastPrice": 5.07,
"lastSize": 2000,
"lastId": "N",
"openPrice": 5,
"highPrice": 5.09,
"lowPrice": 4.915,
"bidTick": " ",
"closePrice": 5.07,
"netChange": 0,
"totalVolume": 54200,
"quoteTimeInLong": 1643418000039,
"tradeTimeInLong": 1643414400004,
"mark": 5.07,
"exchange": "n",
"exchangeName": "NYSE",
"marginable": true,
"shortable": true,
"volatility": 0.2976,
"digits": 2,
"52WkHigh": 9.8,
"52WkLow": 3.8,
"nAV": 0,
"peRatio": 0,
"divAmount": 0,
"divYield": 0,
"divDate": "",
"securityStatus": "Normal",
"regularMarketLastPrice": 5.07,
"regularMarketLastSize": 20,
"regularMarketNetChange": 0,
"regularMarketTradeTimeInLong": 1643414400004,
"netPercentChangeInDouble": 0,
"markChangeInDouble": 0,
"markPercentChangeInDouble": 0,
"regularMarketPercentChangeInDouble": 0,
"delayed": false,
"realtimeEntitled": true
},
"PTPI": {
"assetType": "EQUITY",
"assetMainType": "EQUITY",
"cusip": "71678J100",
"symbol": "PTPI",
"description": "Petros Pharmaceuticals, Inc. - Common Stock",
"bidPrice": 1.59,
"bidSize": 500,
"bidId": "P",
"askPrice": 1.62,
"askSize": 400,
"askId": "P",
"lastPrice": 1.6199,
"lastSize": 0,
"lastId": "D",
"openPrice": 1.56,
"highPrice": 1.6693,
"lowPrice": 1.45,
"bidTick": " ",
"closePrice": 1.62,
"netChange": -0.0001,
"totalVolume": 922374,
"quoteTimeInLong": 1643417960464,
"tradeTimeInLong": 1643417960466,
"mark": 1.62,
"exchange": "q",
"exchangeName": "NASD",
"marginable": false,
"shortable": false,
"volatility": 0.2206,
"digits": 4,
"52WkHigh": 5.2,
"52WkLow": 1.19,
"nAV": 0,
"peRatio": 0,
"divAmount": 0,
"divYield": 0,
"divDate": "",
"securityStatus": "Normal",
"regularMarketLastPrice": 1.62,
"regularMarketLastSize": 12,
"regularMarketNetChange": 0,
"regularMarketTradeTimeInLong": 1643403600790,
"netPercentChangeInDouble": -0.0062,
"markChangeInDouble": 0,
"markPercentChangeInDouble": 0,
"regularMarketPercentChangeInDouble": 0,
"delayed": false,
"realtimeEntitled": true
}
}
这里是 class 我正在尝试反序列化
public class Qoute
{
public Dictionary<string, Equity> Equity { get; set; }
}
public class Equity
{
public string assetType { get; set; }
public string assetMainType { get; set; }
public string cusip { get; set; }
public string symbol { get; set; }
public string description { get; set; }
public float bidPrice { get; set; }
public int bidSize { get; set; }
public string bidId { get; set; }
public float askPrice { get; set; }
public int askSize { get; set; }
public string askId { get; set; }
public float lastPrice { get; set; }
public int lastSize { get; set; }
public string lastId { get; set; }
public float openPrice { get; set; }
public float highPrice { get; set; }
public float lowPrice { get; set; }
public string bidTick { get; set; }
public float closePrice { get; set; }
public float netChange { get; set; }
public int totalVolume { get; set; }
public long quoteTimeInLong { get; set; }
public long tradeTimeInLong { get; set; }
public float mark { get; set; }
public string exchange { get; set; }
public string exchangeName { get; set; }
public bool marginable { get; set; }
public bool shortable { get; set; }
public float volatility { get; set; }
public int digits { get; set; }
public float _52WkHigh { get; set; }
public float _52WkLow { get; set; }
public float nAV { get; set; }
public float peRatio { get; set; }
public float divAmount { get; set; }
public float divYield { get; set; }
public string divDate { get; set; }
public string securityStatus { get; set; }
public float regularMarketLastPrice { get; set; }
public int regularMarketLastSize { get; set; }
public float regularMarketNetChange { get; set; }
public long regularMarketTradeTimeInLong { get; set; }
public float netPercentChangeInDouble { get; set; }
public float markChangeInDouble { get; set; }
public float markPercentChangeInDouble { get; set; }
public float regularMarketPercentChangeInDouble { get; set; }
public bool delayed { get; set; }
public bool realtimeEntitled { get; set; }
}
}
当我尝试转换为我的 class 的 List<> 时,我收到以下响应:
var x= JsonConvert.DeserializeObject<List<Model.Qoutes.Qoute>>(response);
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List
1[TDAPI.Model.Qoutes.Qoute]' 因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。
要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的`
当我尝试只获取一个对象时,Class Equity
为空:
var x2 = JsonConvert.DeserializeObject<Model.Qoutes.Qoute>(response);
我的理解是 public Dictionary<string, Equity> Equity { get; set; }
应该处理动态 属性 名称。我是否漏掉了一些明显的东西?
假设 json 有一个对象作为根,而不是数组,并且该对象没有 Equity
属性,就像您的 Quote
模型一样,它具有包含要映射到 Equity
的对象的属性,因此请尝试:
var result = JsonConvert.DeserializeObject<Dictionary<string, Equity>>(response);