2

So I want to deserialize a Json reply that looks like this:

{
"Meta Data": {
    "1. Information": "Intraday (1min) prices and volumes",
    "2. Symbol": "OMXS30",
    "3. Last Refreshed": "2018-07-11 10:03:00",
    "4. Interval": "1min",
    "5. Output Size": "Compact",
    "6. Time Zone": "US/Eastern"
},
"Time Series (1min)": {
    "2018-07-11 10:03:00": {
        "1. open": "1526.9352",
        "2. high": "1526.9522",
        "3. low": "1526.6548",
        "4. close": "1526.7195",
        "5. volume": "0"
    },
    "2018-07-11 10:02:00": {
        "1. open": "1526.3879",
        "2. high": "1527.0217",
        "3. low": "1526.3879",
        "4. close": "1526.9825",
        "5. volume": "0"
        }
    }
}

I have the following classes:

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

    [JsonProperty("Time Series (1min)")]
    public TimeSeries timeSeries { get; set; }
}

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

class TimeSeries
{
    [JsonProperty("timestamp")]
    public List<DataValues> dataValues { get; set; }
}

class DataValues
{
    [JsonProperty("1. open")]
    public float open { get; set; }

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

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

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

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

The deserialization of the metadata work, but can't seem to get it to work for the datavalues that are in the timestamps. I think that is because the Json property name is changing with every timestamp.

What I want is the values of every timestamp to be in the list called dataValues.

I am using Newtonsoft.Json.

I am trying to get a value like this:

string result = root.timeSeries.dataValues[0].close.ToString();

The error that I get is: the object reference not set to an instance of an object.

4
  • Does timeseries deserialize correctly here? Commented Jul 13, 2018 at 14:11
  • @JayGould I am not sure. Alltough I've gotten rid of the error message that said that the deserializeation was wrong. Commented Jul 13, 2018 at 14:13
  • aren't you missing a level? looks like your TimeSeries should be a list of objects containing a Datetime object and a DataValues object. Commented Jul 13, 2018 at 14:39
  • 1
    @Hack OP actually has one too many level in their structure. The TimeSeries class is not needed at all Commented Jul 13, 2018 at 14:40

1 Answer 1

4

Since the "keys" of your object change and are not known ahead of time, the best structure for you to use is a Dictionary<string, DataValues> for your timeSeries property and ditch your TimeSeries class:

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

    [JsonProperty("Time Series (1min)")]
    public Dictionary<string, DataValues> timeSeries { get; set; }
}

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

class DataValues
{
    [JsonProperty("1. open")]
    public float open { get; set; }

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

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

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

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

I made a fiddle here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.