我正在反序列化到以下类:
public abstract class Tail_Metrics
{
[DataMember(Order = 1, IsRequired = true)]
public double probability { get; set; }
[DataMember(Order = 2, IsRequired = true)]
public double min { get; set; }
[DataMember(Order = 3)]
public double max { get; set; }
[DataMember(Order = 4, IsRequired = true)]
public double mean { get; set; }
[DataMember(Order = 5)]
public double variance { get; set; }
[DataMember(Order = 6)]
public double skewness { get; set; }
[DataMember(Order = 7)]
public double kurtosis { get; set; }
}
public class Layer_Tail_Metrics : Tail_Metrics { }
如您所见,概率,分钟和均值是必需的,其余是可选的。
我正在从服务器反序列化以下JSON响应:
{
"probability": 0.01,
"mean": 0,
"variance": 0,
"min": 0,
"max": 0
}
我收到Newtonsoft.Json.JsonSerializationException
消息Required property 'probability' not found in JSON. Path '', line 1, position 95.
怎么会这样?该属性存在于JSON中,并且具有正确的类型!
最奇怪的是,如果我从IsRequired
属性中删除DataMember
属性,我将停止获取此异常并且对象完全反序列化。如果其中任何一个将IsRequired
属性设置为true,则该属性将抛出相同的异常。
反序列化代码是:
T retVal = converter.Deserialize<T>(response);
其中typeof(T)
为Layer_Tail_Metrics
converter
是一个实现RestSharp.Deserializers.IDeserializer
的类,如下所示:
using Newtonsoft.Json;
public class RestSharp...Converter : RestSharp.Deserializers.IDeserializer
{
//...
private JsonSerializerSettings deserializerSettings;
public RestSharpDataContractNewtonsoftJsonConverter()
{
Culture = CultureInfo.InvariantCulture;
ContentType = "application/json";
deserializerSettings = new JsonSerializerSettings()
{
Converters = new JsonConverter[]{
new Newtonsoft.Json.Converters.IsoDateTimeConverter()
}
};
}
public T Deserialize<T>(IRestResponse response)
{
return Deserialize<T>(response.Content);
}
public T Deserialize<T>(String json)
{
return JsonConvert.DeserializeObject<T>(json, deserializerSettings);
}
}
这是一个小型独立样本,可以重现错误:
[DataContract()]
public abstract class Tail_Metrics
{
[DataMember(IsRequired = true)]
public double probability { get; set; }
public Tail_Metrics(double probability)
{
this.probability = probability;
}
}
public class Layer_Tail_Metrics : Tail_Metrics
{
public Layer_Tail_Metrics(double probability) : base(probability) { }
}
class TestClass
{
static void Main(string[] args)
{
string json = @"
{
""probability"": 0.01
}";
Layer_Tail_Metrics tm = Newtonsoft.Json.JsonConvert.DeserializeObject<Layer_Tail_Metrics>(json);
}
}
答案 0 :(得分:3)
使用以下控制台程序与JSON.Net v5.0.8(NuGet中的最新版本)对我来说很好用:
class Program
{
static void Main(string[] args)
{
string json = @"
{
""probability"": 0.01,
""mean"": 0,
""variance"": 0,
""min"": 0,
""max"": 0
}";
var tm = JsonConvert.DeserializeObject<Layer_Tail_Metrics>(json);
Console.WriteLine("probability: " + tm.probability);
Console.WriteLine("mean: " + tm.mean);
Console.WriteLine("variance: " + tm.variance);
Console.WriteLine("min: " + tm.min);
Console.WriteLine("max: " + tm.max);
}
[DataContract]
public class Layer_Tail_Metrics : Tail_Metrics
{
}
[DataContract]
public class Tail_Metrics
{
[DataMember(Order = 1, IsRequired = true)]
public double probability { get; set; }
[DataMember(Order = 2, IsRequired = true)]
public double min { get; set; }
[DataMember(Order = 3)]
public double max { get; set; }
[DataMember(Order = 4, IsRequired = true)]
public double mean { get; set; }
[DataMember(Order = 5)]
public double variance { get; set; }
[DataMember(Order = 6)]
public double skewness { get; set; }
[DataMember(Order = 7)]
public double kurtosis { get; set; }
}
}
这是输出:
probability: 0.01
mean: 0
variance: 0
min: 0
max: 0
您发布的JSON是从服务器获得的完整JSON响应,还是包装在外部对象中?如果它被包装在另一个对象中,那么这可以解释问题。例如,如果你的JSON实际上是这样的:
{
"response":
{
"probability": 0.01,
"mean": 0,
"variance": 0,
"min": 0,
"max": 0
}
}
并且您尝试将其反序列化为Tail_Metrics
,然后JSON.Net将无法匹配任何属性,因为它们都是JSON中的一个级别。如果您在属性上设置了“IsRequired = true”,那么这将导致JSON.Net抛出您看到的异常。如果你删除它,那么JSON.Net只会为你的类中的double
属性分配一个默认值零,它在JSON中找不到匹配项。如果你的所有数据在JSON中都是零,那么即使它真的无声地失败,它看起来仍然有效。