[英]Deserializing rest client response from wordpress.org
我在我的电脑上有一个本地托管的wordpress.org。 我已经安装了一个名为json-api的wordpress插件,可以让你从wordpress网站上检索帖子。
我正在运行以下代码:
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);
在内容中有几个键,但我只想将'帖子'转换为c#中的模型
我如何实现这一目标?
编辑:
我找到了一个使用newtonsoft for dot net的解决方案
Newtonsoft.Json.JsonConvert.DeserializeObject<BlogArticleResponse>(articles.Content);
在RestSharp
, Content
是反序列化的。 因此,传递给.Execute<T>
方法的类型必须与响应的结构相同 。
在您的情况下,它看起来像这样:
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; }
...
}
然后,您可以执行以下请求:
var result = client.Execute<BlogArticleResponse>(request);
有关更多信息,请查看文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.