问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/51327025/c-sharp-json-deserializing-properties-with-changing-names