2016-05-31 86 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