我正在尝试将以下 json 字符串反序列化为下面的一系列自定义 C# 类。 我不确定这是否是正确或最好的方法,但缺少对 json 字符串进行查找/替换(看起来不是一个非常干净的方法,必须有比这更好的方法)我'我不知道还能做什么。
"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
但是,我想重新排列和重新组织 json 为:
"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"
}
}
所以我可以创建以下类
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; }
}
这样我就可以反序列化 json 字符串。 我已经对此进行了一些研究,但还没有找到我要找的东西。 我正在使用 Newtonsoft,我也想要使用 Newtonsoft 的答案。 但是,如果有更好的东西,我愿意切换到另一个包。 首先,我想知道这个方法是否可行。 如果没有,我想了解社区成员如何解决这个问题。
这是一个如何使用更简单的类结构反序列化 json 的示例
void Main()
{
string json = "{\"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\"}}}";
var obj = JsonConvert.DeserializeObject<Root>(json).Dump();
JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented).Dump();
}
public class Root
{
[JsonProperty("Meta Data")]
public Dictionary<string,string> MetaData { get; set; }
[JsonProperty("Time Series (Daily)")]
public Dictionary<string, Dictionary<string,string>> TimeSeriesDaily { get; set; }
}
正如您所看到的,它是反序列化的,因此您可以编写自定义 JsonConverter (WriteJson 部分,您可以在其中删除列表数字等。)或者自定义 ContractResolver 来转换为您想要的结构。