Deserializing rest client response from wordpress.org

I have a wordpress.org locally hosted on my pc.
I’ve installed a wordpress plugin called json-api which let you retrieve posts from your wordpress site.

I’m running the following code:

Read More
        var client = new RestClient(BlogArticlesUrl);
        var request = new RestRequest();
        request.Timeout = 5000;
        request.RequestFormat = DataFormat.Json;
        request.Method = Method.GET;
        request.AddParameter("json", "get_tag_posts");
        request.AddParameter("slug", "featured");
        request.AddParameter("count", "3");

        var articles = client.Execute<List<BlogArticleModel>>(request);

After executing the code, in the variable articles I have the following:
enter image description here

Inside the Content there are few keys but I would only like to convert ‘posts’ to a model in c#

How do I acheive that?

EDIT:

I have found a solution using newtonsoft for dot net

Newtonsoft.Json.JsonConvert.DeserializeObject<BlogArticleResponse>(articles.Content);

Related posts

Leave a Reply

1 comment

  1. In RestSharp, the Content is what gets deserialized. So, the type you pass into the .Execute<T> method must be the same structure as the response.

    In your case, it will look something like this:

    public class BlogArticleResponse
    {
        public string status { get; set; }
        public int count { get; set; }
        public int pages { get; set; }
        public BlogTag tag { get; set; }
        ...
    }
    
    public class BlogTag 
    {
        public int id { get; set; }
        public string slug { get; set; }
        public string title { get; set; }
        public string description { get; set; }
        ...
    }
    

    You can then execute the request like this:

    var result = client.Execute<BlogArticleResponse>(request);
    

    For more information, have a look at the documentation.