2016-05-31 79 views
1

我有一个本地托管在我的电脑上的wordpress.org。 我已经安装了一个叫做json-api的wordpress插件,它可以让你从你的WordPress站点检索帖子。反序列化来自wordpress.org的其他客户端响应

我运行下面的代码:

 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); 

执行代码后,变量文章中,我有以下几点: enter image description here

里面的内容有几个按键,但我只是会喜欢将'帖子'转换为c#中的模型#

我该如何取得成就?

编辑:

我发现使用newtonsoft的点网

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

回答

0

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); 

欲了解更多信息,看看在documentation

+0

我是否需要写下所有的参数,或者我可以写出我需要的? – Alon

+0

是的,只有你需要。 –

+0

它不起作用,因为响应返回一个带有“Content”键的json对象,它里面有数据 – Alon