2012-02-07 53 views
3

的反序列化反序列化jsonString我得到錯誤錯誤。system.missingMethodException而:當JSON數組

錯誤是Type 'oodleListingsUser' is not supported for deserialization of an array.

我反序列化的代碼是

string jsonString = new WebClient().DownloadString("http://api.oodle.com/api/v2/listings?key=TEST&region=region_value&category=category_value&format=json"); 

    JavaScriptSerializer ser = new JavaScriptSerializer(); 

    jsonOodleApi p = ser.Deserialize<jsonOodleApi>(jsonString); 

我班jsonOodleApi的認定中是

public class jsonOodleApi 
{ 
    public oodleCurrent current; 
    public oodleListings[] listings; 
    public oodleMeta meta; 

    public string state { get; set; } 

} 

認定中oodleCurrentoodleMeta我不會放棄,因爲這是完美的 !

oodleListings認定中是

定義之
public class oodleListings 
{ 
    public string id { get; set; } 
    public string title { get; set; } 

    public oodleListingsUser user; 

    // I have skipped some of fields because it have no issue at all. 

} 

oodleListingsUser

public class oodleListingsUser 
{ 
    public string id { get; set; } 
    public string url { get; set; } 
    public string name { get; set; } 
    public string photo { get; set; } 
} 

問題是我的jsonString有時僅返回一個的用戶值(類型oodleListingsUser的),有時它會返回用戶數組和som etimes它返回用戶的空值!

當它返回只有一個用戶,它運行完全正常!沒有任何問題。

但是,當它返回的用戶陣列,綻放!發生錯誤Type 'oodleListingsUser' is not supported for deserialization of an array.

即使我已經試過public oodleListingsUser[] user但它給錯誤的

No parameterless constructor defined for type of 'oodleListingsUser[]' 

爲僅返回一個用戶的價值!

現在我該怎麼辦才能解決這個問題?

+0

您是否嘗試過'public oodleListingsUser [] user;'? – 2012-02-07 09:37:36

+0

@ L.B是的!它運行完美,如果jsonString返回用戶數組! 但如果jsonString返回一個用戶,它會給'類型的「oodleListingsUser []」'錯誤 – Chintan 2012-02-07 09:38:45

+0

定義無參數的構造函數,甚至我有參數的構造函數在我的班級代碼!它仍然給錯誤! – Chintan 2012-02-07 09:40:23

回答

4

嘗試:

public oodleListings[] listings = new oodleListings[0]; 

或者使它成爲一個List<oodleListings>可能。

+1

我試過List ,它的工作完全正常!謝謝@leppie – Chintan 2012-02-07 12:10:59

+1

感謝您的反饋。 – leppie 2012-02-07 12:12:01

相關問題