我有一个要反序列化的JSON文件,但是items数组中有一个正在更改的标签,因此如何在类说明中指定它,请参阅下面的类说明。
反序列化时,出现以下消息: GetRapidAPIDataForSymbols中的错误:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [MarketDataProvider.YahooHistory + RapidAPIItem]',因为该类型需要JSON数组(例如[1,2,3])可以正确反序列化。
The JSON:
{
"meta": {
"currency": "EUR",
"symbol": "UZU.DE",
"exchangeName": "GER",
"instrumentType": "EQUITY",
"firstTradeDate": 911462400,
"regularMarketTime": 1604397503,
"gmtoffset": 3600,
"timezone": "CET",
"exchangeTimezoneName": "Europe/Berlin",
"regularMarketPrice": 50.2,
"chartPreviousClose": 51,
"priceHint": 2,
"dataGranularity": "1d",
"range": ""
},
"items": {
"1340002800": {
"date": "18-06-2012",
"open": 19.05,
"high": 19.05,
"low": 19.05,
"close": 19.05,
"adjclose": 16.09
},
"1340089200": {
"date": "19-06-2012",
"open": 19.04,
"high": 19.05,
"low": 19.04,
"close": 19.04,
"adjclose": 16.09
},
"1340175600": {
"date": "20-06-2012",
"open": 19.04,
"high": 19.04,
"low": 19.04,
"close": 19.04,
"adjclose": 16.09
},
"1340262000": {
"date": "21-06-2012",
"open": 19.05,
"high": 19.05,
"low": 19.05,
"close": 19.05,
"adjclose": 16.09
},
"1604397503": {
"date": "03-11-2020",
"open": 50.6,
"high": 50.6,
"low": 50.2,
"close": 50.2,
"adjclose": 50.2
}
},
"error": null
}
My classes:
public class Meta
{
public string currency { get; set; }
public string symbol { get; set; }
public string exchangeName { get; set; }
public string instrumentType { get; set; }
public long? firstTradeDate { get; set; }
public long? regularMarketTime { get; set; }
public int? gmtoffset { get; set; }
public string timezone { get; set; }
public string exchangeTimezoneName { get; set; }
public decimal? regularMarketPrice { get; set; }
public decimal? chartPreviousClose { get; set; }
public decimal? previousClose { get; set; }
public int? scale { get; set; }
public int? priceHint { get; set; }
public string dataGranularity { get; set; }
public string range { get; set; }
}
public class RapidAPIHistoryResponse
{
[XmlElement("meta")]
public Meta meta { get; set; }
[XmlElement("items")]
public List<RapidAPIItem> items { get; set; }
public object error { get; set; }
}
public class RapidAPIItem
{
string dateTag { get; set; }
public YahooPrice item { get; set; }
}
My code:
var client = new RestClient("https://yahoo-finance15.p.rapidapi.com/api/yahoo/hi/history/UZU.DE/1d);
var request = new RestRequest(Method.GET);
request.AddHeader("x-rapidapi-host", "yahoo-finance15.p.rapidapi.com");
request.AddHeader("x-rapidapi-key", StartParameters.RapidAPIKey);
IRestResponse response = client.Execute(request);
YahooHistory.RapidAPIHistoryResponse history = JsonConvert.DeserializeObject<YahooHistory.RapidAPIHistoryResponse>(response.Content);
答案 0 :(得分:0)
只要您遵守《物品词典》的钥匙:
public Dictionary<string, Item> Items { get; set; }
在示例中:
"items": {
"1340002800": {
//...
},
"1340089200": {
//...
},
1340002800和1340089200是C#类型的Dictionary的键,如下所示:
Dictionary<string, T> myDictionary
因此,您需要键为字符串,然后是自定义对象。
这还将使您可以处理“列表”中的每个项目:
var myResult = yahooQuotes.Items.Where(w => w.Item.High - w.Item.Low > 5).Select(s => s.Key);
答案 1 :(得分:-1)
在Json中,您具有“项目”:{....}用“ {}”表示项目是一个对象。但是您希望“项目”成为“ []”。所以你杰森应该更像
You passed an id of value {{id}}
}]