【问题标题】:Stuck with DateTime Object name in Json API call在 Json API 调用中卡住 DateTime 对象名称
【发布时间】:2019-04-26 02:31:12
【问题描述】:

我正在调用一个 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",
        "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 转换器时,它会给我一个无效类型的对象名称。 那么我应该制作哪种类型的对象,以便我可以定期访问所有数据。 请帮忙。

【问题讨论】:

  • 使用VisualStudio的“编辑->选择性粘贴->将JSON粘贴为类”,它会为你生成必要的类。
  • @SeM,我在 VS2013 中找不到将 JSON 粘贴为类。你能帮帮我吗?

标签: c# asp.net json asp.net-mvc api


【解决方案1】:

你可以使用这样的东西:

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

如果您更改右侧菜单中的选项,则会出现更多代码。您可以将 Output Features 设置为 Complete,并会得到一个非常好的 sn-p。包括用法。在这种情况下,类似下面的内容足以将 json 反序列化为您的自定义类。

var welcome = Welcome.FromJson(jsonString);

希望这会有所帮助!

【讨论】:

  • 谢谢@Karel Tamayo。它工作得更好,并按照我们的期望提供数据。谢谢。
【解决方案2】:

我最近遇到了来自 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 使用编辑 => 选择性粘贴 => 将 Json 粘贴为类,我知道了

How to Paste

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; }
}

【讨论】:

    猜你喜欢
    • 2013-01-19
    • 1970-01-01
    • 2019-09-22
    • 2013-06-01
    • 2019-11-30
    • 1970-01-01
    • 2014-01-02
    • 2012-04-20
    • 1970-01-01
    相关资源
    最近更新 更多