4

我正在反序列化到以下类:

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);
    }
}
4

1 回答 1

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 中都为零,那么即使它真的在默默地失败,它看起来也会正常工作。

于 2013-10-25T16:40:00.687 回答