2015-02-06 50 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