1

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

Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
  • 4
    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 Mar 24 '18 at 02:00
  • 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 day – Camilo Terevinto Mar 24 '18 at 02:25
  • 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 Mar 24 '18 at 02:33
  • 1
    Possible duplicate of [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/questions/24536533/how-can-i-parse-a-json-string-that-would-cause-illegal-c-sharp-identifiers) – Heretic Monkey Mar 24 '18 at 03:19
  • The closest things it mimics is a `Dictionary>>`. Ouch. – Jake H Mar 24 '18 at 03:33
  • Related or duplicate: [JSON to object C# (mapping complex API response to C# object)](https://stackoverflow.com/q/46117869/3744182), [c# parsing time series data](https://stackoverflow.com/q/44327434/3744182), [Parsing Complex JSON with C#](https://stackoverflow.com/q/44863460/3744182), [Deserialize Json using dynamic object or model](https://stackoverflow.com/q/45325531/3744182). These are all about the same alphavantage API. Do those answer your question? – dbc Mar 24 '18 at 05:18

2 Answers2

3

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; }
}
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
0

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; }
}   
Eugene
  • 1