C#Json使用更改的名称反序列化属性 [英] C# Json deserializing properties with changing names
问题描述
所以我想反序列化如下所示的Json回复:
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"
}
}
}
我有以下课程:
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; }
}
元数据的反序列化是有效的,但似乎无法使其适用于时间戳中的数据值.我认为这是因为Json属性名称随每个时间戳变化.
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.
我想要的是每个时间戳记的值都在名为dataValues的列表中.
What I want is the values of every timestamp to be in the list called dataValues.
我正在使用Newtonsoft.Json.
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.
推荐答案
由于对象的键"发生了变化,并且事先不知道,因此,最适合使用的结构是Dictionary<string, DataValues>
作为TimeSeries
类:
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; }
}
我做了一个小提琴在这里
这篇关于C#Json使用更改的名称反序列化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!