我正在调用一个给我一个JSON响应
的API{
"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"
},
现在,我想访问所有数据,因此我需要创建一个对象,但是当我使用Json2cSharp转换器时,它为我提供了一个无效类型的对象名称。 因此,我应该制造哪种类型的对象,以便可以定期访问所有数据。 请帮忙。
答案 0 :(得分:2)
您可以使用类似这样的内容:
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; }
}
棘手的部分是Intraday
属性,因为必须使用字典才能正确获取所有值。
我使用过quicktype(现在json2csharp与之合作)。如果您想使用该工具玩一点,这里是代码的链接:https://app.quicktype.io?share=DRgQz3PJVCLy4JR3JtGZ
如果您更改右侧菜单中的选项,则那里还有很多代码。您可以将输出功能设置为Complete
,这将得到一个非常好的摘要。包括用法。在这种情况下,如下所示的内容足以将json反序列化为您的自定义类。
var welcome = Welcome.FromJson(jsonString);
希望这会有所帮助!
答案 1 :(得分:0)
我最近在SMS报告API中也遇到了同样的问题,我已经要求他们修改以下对象样式的响应。在DeserializeObject下无法将json数组转换为C#数组对象。所以我更喜欢List数据结构。
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);
更新 @Sem commets使用EDit =>特殊粘贴=>将Json粘贴为类,我得到了
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; }
}