[英]How do i deserialize this JSON containing arrays wrapped in json objects?
我在論壇和許多教程中嘗試了很多答案來反序列化包裹在 json 對象中的 json 數組..但對我來說沒有任何幫助。
我正在嘗試從我的 wordpress 演示站點反序列化這個 json: https : //mahdii.000webhostapp.com/? wpapi = get_posts & dev =1
我想獲得帖子名稱和作者昵稱。
我嘗試過的是:
string json = new WebClient().DownloadString("https://mahdii.000webhostapp.com/?wpapi=get_posts&dev=1");
Console.WriteLine(json);
var deserialize_post = JsonConvert.DeserializeObject<List<RootObject>>(json);
我的課:
public class Author
{
public string id { get; set; }
public string slug { get; set; }
public string name { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string nickname { get; set; }
public string url { get; set; }
public string description { get; set; }
public string gravatar { get; set; }
}
public class Post
{
public string id { get; set; }
public string type { get; set; }
public string slug { get; set; }
public string url { get; set; }
public string status { get; set; }
public string title { get; set; }
public string title_plain { get; set; }
public string date { get; set; }
public string modified { get; set; }
public string excerpt { get; set; }
public string parent { get; set; }
public List<object> category { get; set; }
public List<object> tag { get; set; }
public List<Author> author { get; set; }
public string comment_count { get; set; }
public string comment_status { get; set; }
}
public class RootObject
{
public List<Post> posts { get; set; }
}
我在 deserialize_post 得到 null 我認為我做錯了如果有人可以幫助我,我需要幫助。
謝謝,
我做了一個 dotnetfiddle 示例來解決您的序列化問題:
https://dotnetfiddle.net/kpnYv0
從本質上講,我獲取了您的數據(您的網站目前正在接受死亡的擁抱)並將其放入以下邏輯中:
public static void Main()
{
string json = new WebClient().DownloadString("https://mahdii.000webhostapp.com/?wpapi=get_posts&dev=1");
var deserializePost = JsonConvert.DeserializeObject<RootObject>(json);
var postInfo = deserializePost
.posts
.Select(p => new
{
Name = p.title_plain,
AuthorNicknames = p
.author
.Select(a => a.nickname)
});
Console.WriteLine(postInfo.Count());
}
我只將特別請求的數據投影到postInfo
變量中。 你會發現,每一個職位有多個作者,所以AuthorNicknames
財產postInfo
也是一個IEnumerable。
對於任何試圖提供幫助的人,這里是該站點返回的 JSON:
{
"status": "ok",
"count": 2,
"count_total": 2,
"pages": 1,
"posts": [
{
"id": "9",
"type": "post",
"slug": "e5t-hal-3al2a",
"url": "https:\/\/mahdii.000webhostapp.com\/?p=9",
"status": "publish",
"title": "e5t hal 3al2a",
"title_plain": "e5t hal 3al2a",
"date": "2017-02-09 19:35:05",
"modified": "2017-02-09 19:35:05",
"excerpt": "",
"parent": "0",
"category": [
{
"term_id": 1,
"name": "Uncategorized",
"slug": "uncategorized",
"term_group": 0,
"term_taxonomy_id": 1,
"taxonomy": "category",
"description": "",
"parent": 0,
"count": 1,
"filter": "raw",
"cat_ID": 1,
"category_count": 1,
"category_description": "",
"cat_name": "Uncategorized",
"category_nicename": "uncategorized",
"category_parent": 0
}
],
"tag": [
],
"author": [
{
"id": "1",
"slug": "mahdi",
"name": "mahdi",
"first_name": "",
"last_name": "",
"nickname": "mahdi",
"url": "",
"description": "",
"gravatar": "http:\/\/www.gravatar.com\/avatar\/b2f7652b7fd5e51e1ac71f6d23998c4c?s=100&d=mm&r=g"
}
],
"comment_count": "0",
"comment_status": "open"
},
{
"id": "2",
"type": "page",
"slug": "sample-page",
"url": "https:\/\/mahdii.000webhostapp.com\/?page_id=2",
"status": "publish",
"title": "Sample Page",
"title_plain": "Sample Page",
"date": "2017-02-09 12:31:52",
"modified": "2017-02-09 12:31:52",
"excerpt": "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with … <a href=\"https:\/\/mahdii.000webhostapp.com\/?page_id=2\"> Continue reading <span class=\"meta-nav\">→<\/span><\/a>",
"parent": "0",
"category": [
],
"tag": [
],
"author": [
{
"id": "1",
"slug": "mahdi",
"name": "mahdi",
"first_name": "",
"last_name": "",
"nickname": "mahdi",
"url": "",
"description": "",
"gravatar": "http:\/\/www.gravatar.com\/avatar\/b2f7652b7fd5e51e1ac71f6d23998c4c?s=100&d=mm&r=g"
}
],
"comment_count": "0",
"comment_status": "closed"
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.