33

我在使用 RestSharp 将返回内容反序列化到我的类中时遇到问题。从我所有的搜索来看,我似乎做得对。我宁愿使用 RestSharp 的反序列化器,也不愿回退到像 Newstonsoft 的 Json.NET 这样的另一个包。

我正在做的是向 GoToWebinar 发出 API 请求以获取所有已安排的网络研讨会列表:

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"
   }
]

我使用上面的 JSON 结果使用http://json2csharp.com创建了以下对象:

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 循环遍历结果:

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

关于反序列化为什么失败的任何想法?

4

3 回答 3

91

根据上面@agarcian 的建议,我用谷歌搜索了错误:

restsharp 根级别的数据无效。第 1 行,位置 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 的文档中更清楚地引用。希望这会帮助别人!

于 2012-04-27T16:27:31.487 回答
4

迟到:您需要找到您收到的响应的实际内容类型。服务器不一定会使用您请求的 Accept 标头中的任何内容类型进行响应。对于谷歌的 API,我得到了一个文本/简单的响应,所以这个小组的建议对我有用。

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());
    ...

如果它可以在一个地方完成,例如上面的共享 Execute 方法,而不是在每个请求创建的地方强制使用 OnBeforeDeserialization 响应类型,它也会更整洁。

于 2016-01-06T05:30:21.277 回答
3

我遇到了同样的问题,上面的答案都没有帮助。

这是我的响应类:

public class SomeResponse
{
    public string string1;
    public string string2;
}

问题是人为错误 - 我忘记包含 get/set 访问器。我不经常使用 C# 并且忘记了对它们的需要。

public class SomeResponse
{
    public string string1 { get; set; }
    public string string2 { get; set; }
}

希望这可以帮助那里的人。

于 2020-11-10T17:26:14.103 回答