C# 在Json API调用中使用DateTime对象名
我正在调用一个API,它会给我一个json响应,比如C# 在Json API调用中使用DateTime对象名,c#,asp.net,json,asp.net-mvc,api,C#,Asp.net,Json,Asp.net Mvc,Api,我正在调用一个API,它会给我一个json响应,比如 { "symbol": "AAPL", "stock_exchange_short": "NASDAQ", "timezone_name": "America/New_York", "intraday": { "2018-11-21 15:59:00": { "open": "177.24", "close": "176.77", "high": "177.25", "l
{
"symbol": "AAPL",
"stock_exchange_short": "NASDAQ",
"timezone_name": "America/New_York",
"intraday": {
"2018-11-21 15:59:00": {
"open": "177.24",
"close": "176.77",
"high": "177.25",
"low": "176.77",
"volume": "430073"
},
"2018-11-21 15:58:00": {
"open": "177.23",
"close": "177.23",
"high": "177.25",
"low": "177.12",
"volume": "188425"
},
"2018-11-21 15:57:00": {
"open": "177.18",
"close": "177.21",
"high": "177.24",
"low": "177.11",
"volume": "163151"
},
现在我想访问所有数据,所以我需要创建一个这样的对象,但是当我使用JSON2Charp converter时,它会给我一个无效类型的对象名。
因此,我应该创建哪种类型的对象,以便定期访问所有数据。
请帮助。您可以使用以下内容:
public partial class Welcome
{
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("stock_exchange_short")]
public string StockExchangeShort { get; set; }
[JsonProperty("timezone_name")]
public string TimezoneName { get; set; }
[JsonProperty("intraday")]
public Dictionary<string, Intraday> Intraday { get; set; }
}
public partial class Intraday
{
[JsonProperty("open")]
public string Open { get; set; }
[JsonProperty("close")]
public string Close { get; set; }
[JsonProperty("high")]
public string High { get; set; }
[JsonProperty("low")]
public string Low { get; set; }
[JsonProperty("volume")]
public long Volume { get; set; }
}
希望这有帮助 我最近在SMS报告API中遇到了同样的问题,我要求他们修改对以下对象样式的响应。在反序列化对象下,无法将json数组转换为C#数组对象。所以我更喜欢列表数据结构
public class APIResponse
{
public string symbol { get; set; }
public string stock_exchange_short { get; set; }
public string timezone_name { get; set; }
public List<IntradayLog> intraday { get; set; }
}
public class IntradayLog
{
public float open { get; set; }
public float close { get; set; }
public float high { get; set; }
public float low { get; set; }
public int volume { get; set; }
public DateTime Date { get; set; }
}
var apiLogJson = JsonConvert.DeserializeObject<APIResponse>(myAPIResponse);
使用VisualStudio的“编辑->粘贴特殊->将JSON粘贴为类”,它将为您生成必要的类。@SeM,我在VS2013中找不到将JSON粘贴为类。你能帮我吗?谢谢你@Karel Tamayo。它工作得更好,并按照我们的期望提供数据。非常感谢。
public class APIResponse
{
public string symbol { get; set; }
public string stock_exchange_short { get; set; }
public string timezone_name { get; set; }
public List<IntradayLog> intraday { get; set; }
}
public class IntradayLog
{
public float open { get; set; }
public float close { get; set; }
public float high { get; set; }
public float low { get; set; }
public int volume { get; set; }
public DateTime Date { get; set; }
}
var apiLogJson = JsonConvert.DeserializeObject<APIResponse>(myAPIResponse);
public class Rootobject
{
public string symbol { get; set; }
public string stock_exchange_short { get; set; }
public string timezone_name { get; set; }
public Intraday intraday { get; set; }
}
public class Intraday
{
public _20181121155900 _20181121155900 { get; set; }
public _20181121155800 _20181121155800 { get; set; }
public _20181121155700 _20181121155700 { get; set; }
}
public class _20181121155900
{
public string open { get; set; }
public string close { get; set; }
public string high { get; set; }
public string low { get; set; }
public string volume { get; set; }
}
public class _20181121155800
{
public string open { get; set; }
public string close { get; set; }
public string high { get; set; }
public string low { get; set; }
public string volume { get; set; }
}
public class _20181121155700
{
public string open { get; set; }
public string close { get; set; }
public string high { get; set; }
public string low { get; set; }
public string volume { get; set; }
}