Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何反序列化同一类的不同JSON表示?_C#_Arrays_Serialization_Json.net - Fatal编程技术网

C# 如何反序列化同一类的不同JSON表示?

C# 如何反序列化同一类的不同JSON表示?,c#,arrays,serialization,json.net,C#,Arrays,Serialization,Json.net,我有一个C#类,客户机可能会在两种不同的JSON模式中表示,具体取决于上下文 public class Point { [JsonProperty("x")] public double? X { get; set; } [JsonProperty("y")] public double? Y { get; set; } } public class Multipoint {

我有一个C#类,客户机可能会在两种不同的JSON模式中表示,具体取决于上下文

    public class Point
    {
        [JsonProperty("x")]
        public double? X { get; set; }

        [JsonProperty("y")]
        public double? Y { get; set; }
    }

    public class Multipoint
    {
        [JsonProperty("points")]
        public List<Point> Points { get; set; }
    }
公共类点
{
[JsonProperty(“x”)]
公共双精度X{get;set;}
[JsonProperty(“y”)]
公共双精度?Y{get;set;}
}
公共类多点
{
[JsonProperty(“点”)]
公共列表点{get;set;}
}
其中,
Multipoint
只是一个
点的列表

点的JSON语法为:

{“x”:,“y”:}

我可以使用Json.NET成功地反序列化它

但是,
多点
的JSON语法是

{“点”:[,],[,],…]}

对此,我在尝试反序列化时遇到以下错误:

无法将当前JSON数组(例如[1,2,3])反序列化为类型“Deserialization.Point”,因为该类型需要一个JSON对象(例如{“name”:“value”})才能正确反序列化。 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”}),或将反序列化类型更改为数组或实现可从JSON数组反序列化的集合接口(例如ICollection、IList)类似列表的类型。还可以将JsonArrayAttribute添加到类型中,以强制它从JSON数组反序列化

对,是的,因为序列化程序不知道如何将内部数组的元素(纵坐标对)分配给X和Y属性


因此,这可以通过创建一个自定义派生器来完成,以将正确的纵坐标指定给正确的属性。但是,我不明白的是如何为我的类型分配两个不同的序列化程序。

可能是
多点.Points
属性的自定义
ItemConverterType

public class Multipoint
{
    [JsonProperty(PropertyName = "points", ItemConverterType = typeof(MyPointsConverter))]
    public List<Point> Points { get; set; }
}
公共类多点
{
[JsonProperty(PropertyName=“points”,ItemConverterType=typeof(MyPointsConverter))]
公共列表点{get;set;}
}

转换器应该为
多点.Points
属性实现。

可能是一个自定义的
ItemConverterType

public class Multipoint
{
    [JsonProperty(PropertyName = "points", ItemConverterType = typeof(MyPointsConverter))]
    public List<Point> Points { get; set; }
}
公共类多点
{
[JsonProperty(PropertyName=“points”,ItemConverterType=typeof(MyPointsConverter))]
公共列表点{get;set;}
}

转换器应该实现。

请附加应反序列化的真实JSON代码段。这是一种无需任何自定义序列化程序即可反序列化的结构。实际JSON结构和类之间可能存在不匹配。尝试将JSON粘贴到此处:请附加应反序列化的真实JSON片段。这是一种无需任何自定义序列化程序即可反序列化的结构。实际JSON结构和类之间可能存在不匹配。尝试将JSON粘贴到这里:非常好。非常感谢。这种方法对我有效。但是,我不太明白为什么它反序列化一个点,而不是一个列表。太好了。非常感谢。这种方法对我有效。然而,我不太明白为什么它反序列化一个点,而不是一个列表。