2012-04-25 100 views
28

我遇到了RestSharp反序列化返回內容到我的類的問題。從我所有的搜索中,似乎我正確地做到了這一點。我寧願使用RestSharp的反序列化器,也不願意回退到像Newstonsoft的Json.NET這樣的其他軟件包。RestSharp不反序列化JSON對象列表,始終爲空

什麼我做的是做一個API請求GoToWebinar爲計劃網絡研討會的全部名單:

var client = new RestClient(string.Format("https://api.citrixonline.com/G2W/rest/organizers/{0}/upcomingWebinars", "300000000000239000")); 
var request = new RestRequest(Method.GET); 
request.AddHeader("Authorization", "OAuth oauth_token=" + System.Configuration.ConfigurationManager.AppSettings["GoToWebinar"]); 
var response2 = client.Execute<List<RootObject>>(request); 

正如你看到的,我想獲得對象RootObject「列表(如下圖所示)。我在response2.Content接收以下JSON響應:

[ 
    { 
     "webinarKey":678470607, 
     "subject":"Easton's Wild Rice Cooking Demo", 
     "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
     "organizerKey":300000000000239551, 
     "times":[{"startTime":"2012-05-09T15:00:00Z","endTime":"2012-05-09T16:00:00Z"}], 
     "timeZone":"America/Denver" 
    }, 
    { 
     "webinarKey":690772063, 
     "subject":"Easton's Match Making Service", 
     "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
     "organizerKey":300000000000239551, 
     "times":[{"startTime":"2012-05-09T15:00:00Z","endTime":"2012-05-09T16:00:00Z"}], 
     "timeZone":"America/Denver" 
    } 
] 

我創建使用http://json2csharp.com使用上述JSON結果中的下列對象:

public class RootObject 
{ 
    public int webinarKey { get; set; } 
    public string subject { get; set; } 
    public string description { get; set; } 
    public long organizerKey { get; set; } 
    public List<Time> times { get; set; } 
    public string timeZone { get; set; } 
} 

public class Time 
{ 
    public string startTime { get; set; } 
    public string endTime { get; set; } 
} 

問題是response2.Data總是空。出於某種原因,反序列化失敗了,我不知道爲什麼。我的目標是能夠使用foreach循環遍歷結果:

foreach(RootObject r in response2.Data) 
{ 
    lblGoToWebinar.Text += r.webinarKey.ToString() + ", "; 
} 

爲什麼反序列化失敗的任何想法?

預先感謝您!

+2

你可以調試並查看'response2'對象來查看什麼是原始值?你應該看看調試器是否有錯誤。 – agarcian 2012-04-26 18:00:37

+1

謝謝@agarcian!根據你的建議,我開始搜索一些內部錯誤,並在下面的答案中看到它。 – 2012-04-27 16:29:43

+0

很高興它解決了! – agarcian 2012-04-29 01:10:25

回答

71

基礎上@ agarcian的建議以上,我一派錯誤:

restsharp Data at the root level is invalid. Line 1, position 1.

,發現這個論壇:http://groups.google.com/group/restsharp/browse_thread/thread/ff28ddd9cd3dde4b

基本上,我是錯誤的假設client.Execute是要能夠自動檢測返回的內容類型。它需要明確設置:

var request = new RestRequest(Method.GET); 
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; 

這可以在RestSharp的文檔中更清楚地引用。希望這會幫助別人!

+2

指定'Method.GET'是多餘的,不是嗎? (這是默認的方法類型) – ashes999 2013-04-18 15:25:31

+0

@ ashes999,同意。 – 2013-04-22 21:36:35

+3

根據線程推薦的方法是:'client.AddHandler(「text/plain」,new JsonDeserializer())',這真的應該在文檔中 – Rowan 2013-10-11 05:09:02

1

晚會:您需要找到實際您收到的回覆的內容類型。服務器不一定會響應請求的Accept頭中的任何內容類型。對於Google的API,我得到了一個文本/平原響應,所以這advice from the group爲我工作。

public T Execute<T>(string url, RestRequest request) where T : new() 
{ 
    var client = new RestClient(); 
    // tell RestSharp to decode JSON for APIs that return "Content-Type: text/plain" 
    client.AddHandler("text/plain", new JsonDeserializer()); 
    ... 

這也是整潔,如果它可以在一個地方進行,如共享執行上述方法中,而不是強迫響應類型與OnBeforeDeserialization哪裏創建的每個請求。