RestSharp不反序列化JSON对象列表,总是为空 [英] RestSharp not deserializing JSON Object List, always Null

查看:424
本文介绍了RestSharp不反序列化JSON对象列表,总是为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了RestSharp将返回内容反序列化到我的类中的问题。从我所有的搜索来看,我似乎正确地做到了这一点。我宁愿使用RestSharp的反序列化器而不是像Newstonsoft的Json.NET那样回到另一个包。

I'm having a problem with RestSharp deserializing the return content into my classes. From all my searching it seems that I am doing this correctly. I would much rather use RestSharp's deserializer than have to fall back to another package like Newstonsoft's Json.NET.

我正在做的是向GoToWebinar发送所有列表的API请求预定的网络研讨会:

What I am doing is making a API request to GoToWebinar for all list of scheduled Webinars:

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响应:

As you see I would like to get a list of object 'RootObject' (as shown below). I am receiving the following JSON response in response2.Content:

[
   {
      "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结果:

I created the following objects using http://json2csharp.com using the JSON results above:

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始终为Null。出于某种原因,反序列化失败了,我不知道为什么。我的目标是能够使用foreach循环来遍历结果:

The problem is response2.Data is always Null. For some reason the deserialization failed and I do not know why. My goal is to be able to use a foreach loop to iterate through the results:

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

关于反序列化失败原因的任何想法?

Any ideas on why the deserialization is failing?

提前谢谢!

推荐答案

基于@ agarcian的上面的建议,我用Google搜索了错误:

Based on the @agarcian's suggestion above, I googled the error:


restsharp根级别的数据无效。第1行,第1位。

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

并找到此论坛: http://groups.google.com/group/restsharp/browse_thread/thread/ff28ddd9cd3dde4b

基本上,我认为client.Execute能够自动检测返回内容类型是错误的。它需要明确设置:

Basically, I was wrong to assume that client.Execute was going to be able to auto-detect the return content type. It needs to be explicity set:

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

这可以在RestSharp的文档中更清楚地引用。希望这会帮助其他人!

This could be cited more clearly in RestSharp's documentation. Hopefully this will help someone else out!

这篇关于RestSharp不反序列化JSON对象列表,总是为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆