Deserialize nested JSON Response with RestSharp Client

ⅰ亾dé卋堺 提交于 2019-12-05 21:12:35

Try using this as POCO:

public class Course
{
    public bool success { get; set; }
    public string message { get; set; }
    public List<CourseTypeContainer> Types { get; set; }
}

Now you have list of CourseTypeContainer. And CourseTypeContainer is

public class CourseTypeContainer
{
    public CourseType CourseType { get; set; }
}

So when you are trying to get response.Data.Types[0].AvnetCourseType , then you need to have field AvnetCourseType inside CourseTypeContainer

Or I think what you want is actually this public List<CourseType> Types { get; set; }, you don't need a container there.

Just in case this helps someone else, I tried everything here and it still didn't work on the current version of RestSharp (106.6.2). RestSharp was completely ignoring the RootElement property as far as I could tell, even though it was at the top level. My workaround was to manually tell it to pull the nested JSON and then convert that. I used JSON.Net to accomplish this.

var response = restClient.Execute<T>(restRequest);
response.Content = JObject.Parse(response.Content)[restRequest.RootElement].ToString();
return new JsonDeserializer().Deserialize<T>(response);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!