2015-02-06 156 views
1

我想在Unity上使用Json.NET来序列化一个类。
多边形包含阵列的 Vector2但我只想要序列x和Vector2类Y的变量,这就是为什么我使用的JsonConverter属性。使用Json.NET序列化Vector2的数组

Sample类:

public class Polygon 
{ 
    public int count { get; set; } 

    [JsonConverter(typeof(Vector2Converter[]))] 
    public Vector2[] points { get; set; } 
} 

它给了我在运行这个错误:

MissingMethodException:未找到方法:“默认的构造不 发现...的ctor(的 ) JsonDotNet.Extras.CustomConverters.Vector2Converter []

任何人有任何建议?

回答

1

[JsonConverter]属性采用转换器的类型,而不是转换器数组的类型。如果您的转换器是专门用来处理整个阵列的序列化,那么你需要像这样指定它:

[JsonConverter(typeof(Vector2Converter))] 
    public Vector2[] points { get; set; } 

如果您转换器的设计序列化数组中的单个项目,则需要使用此语法相反:

[JsonProperty(ItemConverterType=typeof(Vector2Converter))] 
    public Vector2[] points { get; set; } 
+0

感谢Brian的帮助 – John 2015-02-09 12:01:36

+0

没问题;我很高兴你发现我的答案有用。 – 2015-02-09 14:55:10