我试图通过调用API来获取课程列表和它们的版本号。 为了做到这一点,我必须循环并获得每个课程的ID,然后为每个课程ID,我必须循环并获得版本号。 然后对于每个课程ID,我必须循环获取版本号。 许多课程没有版本号,这就会返回。 "Message". "Version with specified id was not found: "没有找到指定ID的版本" 因为我的代码期望的是一个数组,所以它抛出了错误'Cannot deserialize the current JSON object (e.g. {"name": "value"}) into type 'System.Collections.Generic.List`1[getCourseVersions+Class2]',因为该类型需要一个JSON数组(例如[1,2,3])才能正确反序列。"
我的问题是这样的。 在遇到 "没有找到指定ID的版本 "的消息后 我如何强制它继续循环浏览所有课程?
下面是我的代码。
public class Rootobject
{
public Class1[] Property1 { get; set; }
}
public class Class1
{
public string CourseType { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public object Description { get; set; }
public object Notes { get; set; }
public object ExternalId { get; set; }
public object AccessDate { get; set; }
public int? ExpireType { get; set; }
public Expireduration ExpireDuration { get; set; }
public object ExpiryDate { get; set; }
public int? ActiveStatus { get; set; }
public string[] TagIds { get; set; }
public object[] ResourceIds { get; set; }
public object[] EditorIds { get; set; }
public object[] Prices { get; set; }
public object[] CompetencyDefinitionIds { get; set; }
public object[] PrerequisiteCourseIds { get; set; }
public object[] PostEnrollmentCourseIds { get; set; }
public bool? AllowCourseEvaluation { get; set; }
public string CategoryId { get; set; }
public string CertificateUrl { get; set; }
public object Audience { get; set; }
public object Goals { get; set; }
public object Vendor { get; set; }
public object CompanyCost { get; set; }
public object LearnerCost { get; set; }
public object CompanyTime { get; set; }
public object LearnerTime { get; set; }
}
public class Expireduration
{
public int? Years { get; set; }
public int? Months { get; set; }
public int? Days { get; set; }
public int? Hours { get; set; }
}
public class Versions
{
public Class1[] Property1 { get; set; }
}
public class Class2
{
public string Id { get; set; }
public string CourseId { get; set; }
public string CourseName { get; set; }
public string VersionNumber { get; set; }
public string Notes { get; set; }
public DateTime? DateAdded { get; set; }
}
//get authorization token from Absorb
private static string GenerateToken()
{
//do stuff to get authorization token
return token;
}
protected void Page_Load(object sender, EventArgs e)
{
//get the generated token
var strToken = GenerateToken();
//build API call to get curricula data
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var courseClient = new RestClient($"https://urlgoeshere/");
var courseRequest = new RestRequest("mycourses", Method.GET);
courseClient.Timeout = 5000;
courseRequest.AddHeader("Authorization", strToken);
courseRequest.AddHeader("Content-Type", "application/json");
var courseResponse = courseClient.Execute(courseRequest);
var courseContent = courseResponse.Content;
var Curriculum = JsonConvert.DeserializeObject<List<Class1>>(courseContent);
//build API call to get enrooment data for each curriculum
var enrollmentClient = new RestClient($"https://urlgoeshere/");
enrollmentClient.Timeout = 5000;
//loop through each curriculum and get version for each
foreach (var ro in Curriculum)
{
var versionRequest = new RestRequest("courseversions/" + ro.Id, Method.GET);
versionRequest.AddHeader("Authorization", strToken);
versionRequest.AddHeader("Content-Type", "application/json");
var versionResponse = enrollmentClient.Execute(versionRequest);
var versionContent = versionResponse.Content;
//this is where it throws the error
var versionData = JsonConvert.DeserializeObject<List<Class2>>(versionContent);
//loop through courses and get versioning information
foreach (var course in versionData)
{
var id = course.Id;
var courseID = course.CourseId;
var courseName = course.CourseName;
var versionNumber = course.VersionNumber;
var notes = course.Notes;
var dateAdded = course.DateAdded;
//display data on page.
Response.Write("ID: " + id + "<br />");
Response.Write("CourseID: " + courseID + "<br />");
Response.Write("Course Name: " + courseName + "<br />");
Response.Write("Version: " + versionNumber + "<br />");
Response.Write("Notes: " + notes + "<br />");
Response.Write("Date Added: " + dateAdded + "<br />");
}
}
}
}
你在这里有几个选择。
1)检查一些额外的属性在 versionResponse
值,然后再尝试在下一行进行反序列化。RestSharp在响应上提供了一些属性,这些属性除了可以设置内容外,还可以帮助告诉你请求失败了,你可以在ifelse块中使用这些属性来跳过你知道不会工作的行为。
你可以检查响应状态,找出是否有任何错误。 以下属性可以告诉你这些错误。
- IsSuccessful
- 响应状态
- 状态码
- 错误信息
- 错误异常
可能是请求被执行了,你得到了200个OK状态码和一些内容,但输入的Data属性是空的。
http:/restsharp.orgget-helpfaq.html#empty-response。
2) RestSharp还可以使用 Execute<T>
方法,它将以强类型的方式返回对象。然后你可以自定义RestSharp在反序列化失败的情况下的行为,通过指示它使用 FailOnDeserialization
, ThrowOnDeserialization
或 ThrowOnAnyError
与相应的属性。
https:/github.comrestsharpRestSharpwikiRecommended-Usage。http:/restsharp.orgusageexceptions.html。