Я пытаюсь позвонить через API, чтобы получить список курсов и номера их версий. Для этого мне нужно пройти L oop и получить ID каждого курса. Затем для каждого идентификатора курса я должен пройти l oop и получить номера версий. Многие из курсов не имеют номеров версий, что возвращает: «Сообщение»: «Версия с указанным идентификатором не найдена». Поскольку мой код ожидает массив, он выдает ошибку "Невозможно десериализовать текущий JSON объект (например, {" name ":" value "}) в тип" System.Collections.Generi c .List`1 [getCourseVersions + Class2] 'потому что для корректной десериализации типа требуется массив JSON (например, [1,2,3]). "
Мой вопрос таков: как заставить его продолжать цикл по всем курсы после появления сообщения «версия с указанным идентификатором не найдена»?
Вот мой код:
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 />");
}
}
}
}