我想從WordPress的測試網站獲得帖子。當我調試時,我可以看到我從網上獲取數據,但是當我嘗試將它們放入文本框時,它們似乎不存在。我使用了一個網站來創建C#中的類,但我認爲這個問題出現在我的代碼中。我把這樣的方法:C#問題從json轉換
RootObject myPosts = await Class1.Get();
然後,以顯示給用戶的價值,我做的:
textBox1.Text = myPosts.id + " - " + myPosts.author+ "-" + myPosts.title;
代碼:
public class Class1
{
public async static Task<RootObject> Get()
{
var http = new HttpClient();
var response = await http.GetAsync("http://bearlike-attackers.000webhostapp.com/wp-json/wp/v2/posts?search=TEST");
var result = await response.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (RootObject)serializer.ReadObject(ms);
return data;
}
}
[DataContract]
public class Guid
{
[DataMember]
public string rendered { get; set; }
}
[DataContract]
public class Title
{
[DataMember]
public string title { get; set; }
}
[DataContract]
public class Content
{
[DataMember]
public string rendered { get; set; }
[DataMember]
public bool @protected { get; set; }
}
[DataContract]
public class Excerpt
{
[DataMember]
public string rendered { get; set; }
[DataMember]
public bool @protected { get; set; }
}
[DataContract]
public class Self
{
[DataMember]
public string href { get; set; }
}
[DataContract]
public class Collection
{
[DataMember]
public string href { get; set; }
}
[DataContract]
public class About
{
[DataMember]
public string href { get; set; }
}
[DataContract]
public class Author
{
[DataMember]
public bool embeddable { get; set; }
[DataMember]
public string href { get; set; }
}
[DataContract]
public class Reply
{
[DataMember]
public bool embeddable { get; set; }
[DataMember]
public string href { get; set; }
}
[DataContract]
public class VersionHistory
{
[DataMember]
public string href { get; set; }
}
[DataContract]
public class WpAttachment
{
[DataMember]
public string href { get; set; }
}
[DataContract]
public class WpTerm
{
[DataMember]
public string taxonomy { get; set; }
[DataMember]
public bool embeddable { get; set; }
[DataMember]
public string href { get; set; }
}
[DataContract]
public class Cury
{
[DataMember]
public string name { get; set; }
[DataMember]
public string href { get; set; }
[DataMember]
public bool templated { get; set; }
}
/*
public class Links
{
public List<Self> self { get; set; }
public List<Collection> collection { get; set; }
public List<About> about { get; set; }
public List<Author> author { get; set; }
public List<Reply> replies { get; set; }
public List<VersionHistory> __invalid_name__version-history { get; set; }
public List<WpAttachment> __invalid_name__wp:attachment { get; set; }
public List<WpTerm> __invalid_name__wp:term { get; set; }
public List<Cury> curies { get; set; }
}
*/
[DataContract]
public class RootObject
{
[DataMember]
public string date { get; set; }
[DataMember]
public int id { get; set; }
[DataMember]
public string date_gmt { get; set; }
[DataMember]
public Guid guid { get; set; }
[DataMember]
public string modified { get; set; }
[DataMember]
public string modified_gmt { get; set; }
[DataMember]
public string slug { get; set; }
[DataMember]
public string type { get; set; }
[DataMember]
public string link { get; set; }
[DataMember]
public Title title { get; set; }
[DataMember]
public Content content { get; set; }
[DataMember]
public Excerpt excerpt { get; set; }
[DataMember]
public int author { get; set; }
[DataMember]
public int featured_media { get; set; }
[DataMember]
public string comment_status { get; set; }
[DataMember]
public string ping_status { get; set; }
[DataMember]
public bool sticky { get; set; }
[DataMember]
public string template { get; set; }
[DataMember]
public string format { get; set; }
/*
[DataMember]
public List<object> meta { get; set; }
[DataMember]
public List<int> categories { get; set; }
[DataMember]
public List<object> tags { get; set; }
[DataMember]
public Links _links { get; set; }
*/
}
}
_「當我試圖把它們放在文本框中,它們似乎不存在」_ - 你能顯示代碼,你試圖顯示它們嗎? – stuartd
textBox1.Text = myPosts.id +「 - 」+ myPosts.author +「 - 」+ myPosts.title; –