【问题标题】:how to de-serialize JSON data in which Timestamp it-self contains fields?如何反序列化时间戳本身包含字段的JSON数据?
【发布时间】:2018-09-30 10:25:53
【问题描述】:

我无法使用给定的 JSON 数据映射类:

{
    "Meta Data": {
        "1. Information": "Intraday (15min) open, high, low, close prices and volume",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2018-09-28 15:45:00",
        "4. Interval": "15min",
        "5. Output Size": "Full size",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (15min)": {
        "2018-09-28 15:45:00": {
            "1. open": "114.2800",
            "2. high": "114.5600",
            "3. low": "114.2400",
            "4. close": "114.4800",
            "5. volume": "2316251"
        },
        "2018-09-28 15:30:00": {
            "1. open": "114.4450",
            "2. high": "114.4500",
            "3. low": "114.2600",
            "4. close": "114.2900",
            "5. volume": "759991"
        },
        "2018-09-28 15:15:00": {
            "1. open": "114.3550",
            "2. high": "114.5200",
            "3. low": "114.3100",
            "4. close": "114.4400",
            "5. volume": "515174"
        }
    }
}

如何创建类结构结构,以便我可以在 c# 中使用 newtonsoft 对上述数据进行反序列化?

【问题讨论】:

  • 字典怎么样,假设你不能有任何重复?
  • 我需要考虑 timestamp(2018-09-28 15:45:00) 本身的类名吗?我认为按时间戳创建类是不好的。

标签: c# json json.net


【解决方案1】:

Quicktype 的帮助和一些编辑的帮助下,我创建了这些类:

public class RootObject
{
    [JsonProperty("Meta Data")]
    public MetaData MetaData { get; set; }

    [JsonProperty("Time Series (15min)")]
    public Dictionary<DateTime, TimeSeriesItem> TimeSeries15Min { get; set; }
}

public class MetaData
{
    [JsonProperty("1. Information")]
    public string The1Information { get; set; }

    [JsonProperty("2. Symbol")]
    public string The2Symbol { get; set; }

    [JsonProperty("3. Last Refreshed")]
    public DateTimeOffset The3LastRefreshed { get; set; }

    [JsonProperty("4. Interval")]
    public string The4Interval { get; set; }

    [JsonProperty("5. Output Size")]
    public string The5OutputSize { get; set; }

    [JsonProperty("6. Time Zone")]
    public string The6TimeZone { get; set; }
}

public class TimeSeriesItem
{
    [JsonProperty("1. open")]
    public string The1Open { get; set; }

    [JsonProperty("2. high")]
    public string The2High { get; set; }

    [JsonProperty("3. low")]
    public string The3Low { get; set; }

    [JsonProperty("4. close")]
    public string The4Close { get; set; }

    [JsonProperty("5. volume")]
    public long The5Volume { get; set; }
}

你可以像这样反序列化它:

var data = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);

Try it online

【讨论】:

  • 非常感谢 .. 是的,它工作正常。 :) 这对我真的很有帮助,因为我刚刚开始研究 JSON。
【解决方案2】:

这是解决方案,包括测试...我通常使用this Json Class Jenerator 来生成类,有时您确实需要修改它输出的类,但总的来说,它非常好

void Main()
{
    string test = @"{
    ""Meta Data"": {
        ""1. Information"": ""Intraday (15min) open, high, low, close prices and volume"",
        ""2. Symbol"": ""MSFT"",
        ""3. Last Refreshed"": ""2018-09-28 15:45:00"",
        ""4. Interval"": ""15min"",
        ""5. Output Size"": ""Full size"",
        ""6. Time Zone"": ""US/Eastern""
    },
    ""Time Series (15min)"": {
        ""2018-09-28 15:45:00"": {
            ""1. open"": ""114.2800"",
            ""2. high"": ""114.5600"",
            ""3. low"": ""114.2400"",
            ""4. close"": ""114.4800"",
            ""5. volume"": ""2316251""
        },
        ""2018-09-28 15:30:00"": {
            ""1. open"": ""114.4450"",
            ""2. high"": ""114.4500"",
            ""3. low"": ""114.2600"",
            ""4. close"": ""114.2900"",
            ""5. volume"": ""759991""
        },
        ""2018-09-28 15:15:00"": {
            ""1. open"": ""114.3550"",
            ""2. high"": ""114.5200"",
            ""3. low"": ""114.3100"",
            ""4. close"": ""114.4400"",
            ""5. volume"": ""515174""
        }
    }
}";

    JsonConvert.DeserializeObject<Result>(test).Dump();

}

// Define other methods and classes here
public class MetaData
{
    [JsonProperty("1. Information")]
    public string Information { get; set; }

    [JsonProperty("2. Symbol")]
    public string Symbol { get; set; }

    [JsonProperty("3. Last Refreshed")]
    public string LastRefreshed { get; set; }

    [JsonProperty("4. Interval")]
    public string Interval { get; set; }

    [JsonProperty("5. Output Size")]
    public string OutputSize { get; set; }

    [JsonProperty("6. Time Zone")]
    public string TimeZone { get; set; }
}

public class CandleStick
{

    [JsonProperty("1. open")]
    public string Open { get; set; }

    [JsonProperty("2. high")]
    public string High { get; set; }

    [JsonProperty("3. low")]
    public string Low { get; set; }

    [JsonProperty("4. close")]
    public string Close { get; set; }

    [JsonProperty("5. volume")]
    public string Volume { get; set; }
}


public class Result
{

    [JsonProperty("Meta Data")]
    public MetaData MetaData { get; set; }

    [JsonProperty("Time Series (15min)")]
    public Dictionary<DateTime, CandleStick> TimeSeries15min { get; set; }
}

【讨论】:

    猜你喜欢
    • 2017-09-08
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多