I’m trying to deserialize the following json string to a series of custom c# classes below. I’m not sure if this is the right or best approach but short of doing a Find/Replace on the json string(doesn’t seem like a very clean way to do it, there must be a better way than that) I’m not sure what else can be done.
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "NVDA",
"3. Last Refreshed": "2024-12-06",
"4. Output Size": "Full size",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2024-12-06": {
"1. open": "144.6000",
"2. high": "145.7000",
"3. low": "141.3100",
"4. close": "142.4400",
"5. volume": "188505573"
},
"2024-12-05": {
"1. open": "145.1100",
"2. high": "146.5400",
"3. low": "143.9500",
"4. close": "145.0600",
"5. volume": "172621180"
},
// Many more entries
But, I want to rearrange and reorganize the json to:
"MetaData": {
"Information": "Daily Prices (open, high, low, close) and Volumes",
"Symbol": "NVDA",
"LastRefreshed": "2024-12-06",
"OutputSize": "Full size",
"TimeZone": "US/Eastern"
},
"TimeSeries": {
"Data": {
"open": "144.6000",
"high": "145.7000",
"low": "141.3100",
"close": "142.4400",
"volume": "188505573",
"date": "2024-12-06",
"series": "Daily"
},
"Data": {
"open": "145.1100",
"high": "146.5400",
"low": "143.9500",
"close": "145.0600",
"volume": "172621180",
"date": "2024-12-05",
"series": "Daily"
}
}
So I can create the following classes
public class Data
{
[JsonProperty("open")]
public string open { get; set; }
[JsonProperty("high")]
public string high { get; set; }
[JsonProperty("low")]
public string low { get; set; }
[JsonProperty("close")]
public string close { get; set; }
[JsonProperty("volume")]
public string volume { get; set; }
[JsonProperty("date")]
public string date { get; set; }
[JsonProperty("series")]
public string series { get; set; }
}
public class Root
{
[JsonProperty("MetaData")]
public MetaData MetaData { get; set; }
[JsonProperty("TimeSeries")]
public TimeSeries TimeSeries { get; set; }
}
public class TimeSeries
{
[JsonProperty("Data")]
public Data Data { get; set; }
}
public class MetaData
{
[JsonProperty("Information")]
public string Information { get; set; }
[JsonProperty("Symbol")]
public string Symbol { get; set; }
[JsonProperty("Last Refreshed")]
public string LastRefreshed { get; set; }
[JsonProperty("Output Size")]
public string OutputSize { get; set; }
[JsonProperty("Time Zone")]
public string TimeZone { get; set; }
}
This way I could deserialize the json string. I’ve done some research looking in to this but I haven’t found what I’m looking for yet. I’m using Newtonsoft and I’d like answers to use Newtonsoft as well. But, if there is something better out there I’ll be willing to switch to another package. First, I’d like to know if this approach would work. If not, I’d like ideas on how communiy members would tackle this problem.