Eas*_*vey 30 .net c# asp.net rest http
我在使用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);
Run Code Online (Sandbox Code Playgroud)
如您所见,我想获得一个对象'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"
}
]
Run Code Online (Sandbox Code Playgroud)
我使用上面的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; }
}
Run Code Online (Sandbox Code Playgroud)
问题是response2.Data总是空的.出于某种原因,反序列化失败了,我不知道为什么.我的目标是能够使用foreach循环来迭代结果:
foreach(RootObject r in response2.Data)
{
lblGoToWebinar.Text += r.webinarKey.ToString() + ", ";
}
Run Code Online (Sandbox Code Playgroud)
关于反序列化失败原因的任何想法?
Eas*_*vey 80
基于@ 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"; };
Run Code Online (Sandbox Code Playgroud)
这可以在RestSharp的文档中更清楚地引用.希望这会帮助其他人!
归档时间: |
|
查看次数: |
25542 次 |
最近记录: |