1
votes

How to create the model for this kind of json, have number in properties, date as object etc...

{
    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "AAP",
        "3. Last Refreshed": "2018-03-23",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2018-03-23": {
            "1. open": "112.4100",
            "2. high": "113.2600",
            "3. low": "110.3400",
            "4. close": "110.8400",
            "5. volume": "1085896"
        },
        "2018-03-22": {
            "1. open": "114.0200",
            "2. high": "115.1400",
            "3. low": "111.6300",
            "4. close": "111.7100",
            "5. volume": "1038170"
        }
    }
}

See where I will get this kind of json:

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo

2
Whoever came up with that needs to be slapped. Sorry for your suffering. Do you have to use this data source for stock symbols?Ron Beyer
I love that they made arrays as objects. I think you'll need to use dynamic JSON, if you used classes you'd have to rebuild every dayCamilo Terevinto
I've unfortunately had to deal with this before and it sucks. I was able to solve it using anonymous objects and lots of dictionary types.BinaryPatrick
The closest things it mimics is a Dictionary<string,Dictionary<string,Dictionary<string,string>>>. Ouch.Jake H

2 Answers

3
votes

Finally Created this, may it can help anyone

public class StockViewModel
{
    [JsonProperty(PropertyName = "Meta Data")]
    public MetaData metaData { get; set; }

    [JsonProperty(PropertyName = "Time Series (Daily)")] 
    public Dictionary<DateTime, TimeSeriesIntraDayJsonClass> Data { get; set; }
}

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

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

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

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

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

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

public class TimeSeriesIntraDayJsonClass
{
    [JsonProperty(PropertyName = "1. open")]
    public double open { get; set; }
    [JsonProperty(PropertyName = "2. high")]
    public double high { get; set; }
    [JsonProperty(PropertyName = "3. low")]
    public double low { get; set; }
    [JsonProperty(PropertyName = "4. close")]
    public double close { get; set; }
    [JsonProperty(PropertyName = "5. volume")]
    public double volume { get; set; }
}
0
votes

I know this is an old question, but maybe this can help anyone else looking to convert these absurd json types to classses. Eventhough the naming of the properties can be improved this proves quit handy to point anyone in the right direction

https://app.quicktype.io/

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

    [JsonProperty("Time Series (Daily)")]
    public Dictionary<string, TimeSeriesDaily> TimeSeriesDaily { get; set; }
}

public partial 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. Output Size")]
    public string The4OutputSize { get; set; }

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

public partial class TimeSeriesDaily
{
    [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")]
    [JsonConverter(typeof(ParseStringConverter))]
    public long The5Volume { get; set; }
}