简体   繁体   English

RestSharp 不反序列化 JSON 对象列表,始终为空

[英]RestSharp not deserializing JSON Object List, always Null

I'm having a problem with RestSharp deserializing the return content into my classes.我在 RestSharp 将返回内容反序列化到我的类中时遇到问题。 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.我更愿意使用 RestSharp 的解串器,而不是不得不退回到另一个包,比如 Newstonsoft 的 Json.NET。

What I am doing is making a API request to GoToWebinar for all list of scheduled Webinars:我正在做的是向 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);

As you see I would like to get a list of object 'RootObject' (as shown below).如您所见,我想获取对象“RootObject”的列表(如下所示)。 I am receiving the following JSON response in response2.Content:我在 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"
   }
]

I created the following objects using http://json2csharp.com using the JSON results above:我使用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; }
}

The problem is response2.Data is always Null.问题是 response2.Data 始终为 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 循环遍历结果:

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

Any ideas on why the deserialization is failing?关于为什么反序列化失败的任何想法?

Based on the @agarcian's suggestion above, I googled the error:根据上面@agarcian 的建议,我用谷歌搜索了错误:

restsharp Data at the root level is invalid. restsharp 根级别的数据无效。 Line 1, position 1.第 1 行,位置 1。

and found this forum: http://groups.google.com/group/restsharp/browse_thread/thread/ff28ddd9cd3dde4b并找到了这个论坛: http : //groups.google.com/group/restsharp/browse_thread/thread/ff28ddd9cd3dde4b

Basically, I was wrong to assume that client.Execute was going to be able to auto-detect the return content type.基本上,我假设 client.Execute 将能够自动检测返回内容类型是错误的。 It needs to be explicity set:它需要明确设置:

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

This could be cited more clearly in RestSharp's documentation.这可以在 RestSharp 的文档中更清楚地引用。 Hopefully this will help someone else out!希望这会帮助别人!

Late to the party: You would need to find the actual Content-Type of the response you were getting.迟到:您需要找到您收到的响应的实际内容类型。 The server doesn't necessarily respond with any of the content types from your request's Accept header.服务器不一定会使用您请求的 Accept 标头中的任何内容类型进行响应。 For Google's APIs I got a text/plain response, so this advice from the group worked for me.对于 Google 的 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());
    ...

It's also tidier if it can be done in one place such as the shared Execute method above, rather than forcing the response type with OnBeforeDeserialization wherever each request is created.如果它可以在一个地方完成,例如上面的共享 Execute 方法,而不是在创建每个请求的任何地方使用 OnBeforeDeserialization 强制响应类型,那么它也会更整洁。

I had this same problem, and none of the answers above helped.我遇到了同样的问题,上面的答案都没有帮助。

Here was my response class:这是我的响应类:

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

The problem was human error - I forgot to include the get/set accessors.问题是人为错误 - 我忘记包含 get/set 访问器。 I don't often use C# and forgot the need for them.我不经常使用 C# 并且忘记了对它们的需要。

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

Hope this helps someone out there.希望这可以帮助那里的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM