当名称为动态时反序列化 Json
Deserialize Json when name is dynamic
我用这个简单API,https://exchangeratesapi.io/ and I test with this uri: https://api.exchangeratesapi.io/history?start_at=2018-01-01&end_at=2018-03-01&symbols=SEK。
我想反序列化 'rates' 部分。这是一个响应示例
这是代码
public class ExchangeRate
{
[JsonProperty(PropertyName = "end_at", Order = 1)]
public DateTime EndAt { get; set; }
[JsonProperty(PropertyName = "start_at", Order = 2)]
public DateTime StartAt { get; set; }
[JsonProperty(PropertyName = "rates", Order = 3)]
public Dictionary<string, Rate> Rates { get; set; }
[JsonProperty(PropertyName = "base", Order = 4)]
public string Base { get; set; }
}
public class Rate
{
[JsonProperty]
public Dictionary<string, double> Fields{ get; set; }
}
或
public class Rate
{
[JsonProperty]
public string CurrencyName { get; set; }
[JsonProperty]
public double CurrencyRate { get; set; }
}
我这样反序列化它
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<ExchangeRateHistory>(response.Content);
我的问题是 Fields 为空。有人有什么建议吗?
如果您的 key/value 对不固定并且数据必须是可配置的,那么 Newtonsoft.json 有一个功能可以在这里使用,那就是 [JsonExtensionData]
。 Read more
Extension data is now written when an object is serialized. Reading and writing extension data makes it possible to automatically round-trip all JSON without adding every property to the .NET type you’re deserializing to. Only declare the properties you’re interested in and let extension data do the rest.
在您的情况下,rates
键具有作为动态数据的价值,因此您的 Rate
class 将是
public class Rate
{
[JsonExtensionData]
public Dictionary<string, JToken> Fields { get; set; }
}
然后你可以将你的响应内容反序列化为
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<ExchangeRate>(response.Content);
我用这个简单API,https://exchangeratesapi.io/ and I test with this uri: https://api.exchangeratesapi.io/history?start_at=2018-01-01&end_at=2018-03-01&symbols=SEK。
我想反序列化 'rates' 部分。这是一个响应示例
这是代码
public class ExchangeRate
{
[JsonProperty(PropertyName = "end_at", Order = 1)]
public DateTime EndAt { get; set; }
[JsonProperty(PropertyName = "start_at", Order = 2)]
public DateTime StartAt { get; set; }
[JsonProperty(PropertyName = "rates", Order = 3)]
public Dictionary<string, Rate> Rates { get; set; }
[JsonProperty(PropertyName = "base", Order = 4)]
public string Base { get; set; }
}
public class Rate
{
[JsonProperty]
public Dictionary<string, double> Fields{ get; set; }
}
或
public class Rate
{
[JsonProperty]
public string CurrencyName { get; set; }
[JsonProperty]
public double CurrencyRate { get; set; }
}
我这样反序列化它
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<ExchangeRateHistory>(response.Content);
我的问题是 Fields 为空。有人有什么建议吗?
如果您的 key/value 对不固定并且数据必须是可配置的,那么 Newtonsoft.json 有一个功能可以在这里使用,那就是 [JsonExtensionData]
。 Read more
Extension data is now written when an object is serialized. Reading and writing extension data makes it possible to automatically round-trip all JSON without adding every property to the .NET type you’re deserializing to. Only declare the properties you’re interested in and let extension data do the rest.
在您的情况下,rates
键具有作为动态数据的价值,因此您的 Rate
class 将是
public class Rate
{
[JsonExtensionData]
public Dictionary<string, JToken> Fields { get; set; }
}
然后你可以将你的响应内容反序列化为
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<ExchangeRate>(response.Content);