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:
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:
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);
In
RestSharp
, theContent
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:
You can then execute the request like this:
For more information, have a look at the documentation.