2012-10-11 49 views
0

我需要使用RestSharp讀取以下代碼片段。我的問題是如何獲得適當結構的陣列。我如何設置包含對象的類以使其正常工作?如何使用restsharp讀取Json數組

我想反序列化類型爲AcUserInfo的列表中的對象「0」和「1」。

非常感謝。 安德烈

{ 
    "0":{ 
     "id":"2", 
     "subscriberid":"2", 
     "cdate":"2012-09-28 16:49:06", 
     "sdate":"2012-09-28 16:49:06", 
     "first_name":"Al", 
     "last_name":"", 
     "email":"[email protected]" 
    }, 
    "1":{ 
     "id":"29", 
     "subscriberid":"29", 
     "cdate":"2012-10-02 15:08:29", 
     "sdate":"2012-10-02 15:08:29", 
     "first_name":"Mark", 
     "last_name":"", 
     "email":"[email protected]" 
    }, 
    "result_code":1, 
    "result_message":"Success: Something is returned", 
    "result_output":"json" 
} 

下面是我創建的類:

public class SubscriberList { 
    public int result_code { get; set; } 
    public string result_message { get; set; } 
    public string result_output { get; set; } 
    public List<AcUserInfo> row { get; set; } 

    SubscriberList(){ 
     row = new List<AcUserInfo>(); 
    } 
} 

回答

0

你的JSON數據不包含數組,因此不能進行反序列化到一個列表<>。

無論你的JSON轉換爲這樣的事情:

{ 
    [{ 
    "id":"2", 
    "subscriberid":"2", 
    "cdate":"2012-09-28 16:49:06", 
    "sdate":"2012-09-28 16:49:06", 
    "first_name":"Al", 
    "last_name":"", 
    "email":"[email protected]" 
    }, 
    { 
    "id":"29", 
    "subscriberid":"29", 
    "cdate":"2012-10-02 15:08:29", 
    "sdate":"2012-10-02 15:08:29", 
    "first_name":"Mark", 
    "last_name":"", 
    "email":"[email protected]" 
    }], 
    "result_code":1, 
    "result_message":"Success: Something is returned", 
    "result_output":"json" 
} 

或者,如果你總是隻有那麼「0」 &「1」元素來改變你的SubscriberList類相匹配。

+0

感謝您的信息。這些項目由ActiveCampaign以這種方式返回。我設法解決我的問題返回的項目爲XML。謝謝。 –