Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

c# - how to de-serialize JSON data in which Timestamp it-self contains fields?

I am Unable to map the class with the given JSON data:

{
    "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"
        }
    }
}

How to create class structure structure so that i can de-serialize the above data using newtonsoft in c#?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using the help of Quicktype and a bit of editing, I created these classes:

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

You can deserialize it like so:

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

Try it online


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...