这让我疯了 - 我无法反序列化一些JSON,我正在从Wordpress公共API中获取我的帖子。我正在使用RestSharp。我收到的数据显示了博客文章,但所有属性都为空。我甚至使用Json2CSharp在返回的内容后对我的“Post”类进行建模。
这是我的JSON:
{
"found":1,
"posts":[
{
"ID":2,
"site_ID":89749867,
"author":{
"ID":2988648,
"login":"loginname",
"email":false,
"name":"loginname",
"nice_name":"loginname",
"URL":"",
"avatar_URL":"https:\/\/2.gravatar.com\/avatar\/e78ee2e2f7bb75a8bc5339a6bd20194e?s=96&d=identicon&r=G",
"profile_URL":"http:\/\/en.gravatar.com\/loginname",
"site_ID":2918427
},
"date":"2015-04-20T18:16:18+00:00",
"modified":"2015-04-20T18:18:20+00:00",
"title":"Hello world!",
"URL":"https:\/\/myblog.wordpress.com\/2015\/04\/20\/hello-world\/",
"short_URL":"http:\/\/wp.me\/p64A2v-2",
"content":"<p><a href=\"https:\/\/myblog.files.wordpress.com\/2015\/04\/left_shark_5x3.png\"><img class=\"alignnone size-medium wp-image-3\" src=\"https:\/\/myblog.files.wordpress.com\/2015\/04\/left_shark_5x3.png?w=300&h=180\" alt=\"Left Shark\" width=\"300\" height=\"180\" \/><\/a><\/p>\n<p>This is your very first post. Click the Edit link to modify or delete it, or <a title=\"Direct link to Add New Post in your Dashboard\" href=\"https:\/\/wordpress.com\/post\">start a new post<\/a>. If you like, use this post to tell readers why you started this blog and what you plan to do with it.<\/p>\n<p>Happy blogging!<\/p>\n",
"excerpt":"<p>This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it. Happy blogging!<\/p>\n",
"slug":"hello-world",
"guid":"http:\/\/myblog.wordpress.com\/?p=1",
"status":"publish",
"sticky":false,
"password":"",
"parent":false,
"type":"post",
"discussion":{
"comments_open":true,
"comment_status":"open",
"pings_open":true,
"ping_status":"open",
"comment_count":0
},
"likes_enabled":true,
"sharing_enabled":true,
"like_count":0,
"i_like":0,
"is_reblogged":0,
"is_following":0,
"global_ID":"7a5ad608c4e80f7c2463eead8d34f435",
"featured_image":"https:\/\/myblog.files.wordpress.com\/2015\/04\/left_shark_5x3.png",
"post_thumbnail":{
"ID":3,
"URL":"https:\/\/myblog.files.wordpress.com\/2015\/04\/left_shark_5x3.png",
"guid":"http:\/\/myblog.files.wordpress.com\/2015\/04\/left_shark_5x3.png",
"mime_type":"image\/png",
"width":620,
"height":372
},
"format":"standard",
"geo":false,
"menu_order":0,
"page_template":"",
"publicize_URLs":[
],
"tags":{
},
"categories":{
"Uncategorized":{
"ID":1,
"name":"Uncategorized",
"slug":"uncategorized",
"description":"",
"post_count":1,
"parent":0,
"meta":{
"links":{
"self":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867\/categories\/slug:uncategorized",
"help":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867\/categories\/slug:uncategorized\/help",
"site":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867"
}
}
}
},
"attachments":{
"3":{
"ID":3,
"URL":"https:\/\/myblog.files.wordpress.com\/2015\/04\/left_shark_5x3.png",
"guid":"http:\/\/myblog.files.wordpress.com\/2015\/04\/left_shark_5x3.png",
"date":"2015-04-20T18:17:33+00:00",
"post_ID":2,
"file":"left_shark_5x3.png",
"mime_type":"image\/png",
"extension":"png",
"title":"Left Shark",
"caption":"",
"description":"",
"alt":"Left Shark",
"thumbnails":{
},
"height":372,
"width":620,
"exif":{
"aperture":0,
"credit":"",
"camera":"",
"caption":"",
"created_timestamp":0,
"copyright":"",
"focal_length":0,
"iso":0,
"shutter_speed":0,
"title":"",
"orientation":0
},
"meta":{
"links":{
"self":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867\/media\/3",
"help":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867\/media\/3\/help",
"site":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867",
"parent":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867\/posts\/2"
}
}
}
},
"attachment_count":1,
"metadata":[
{
"id":"9",
"key":"_thumbnail_id",
"value":"3"
}
],
"meta":{
"links":{
"self":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867\/posts\/2",
"help":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867\/posts\/2\/help",
"site":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867",
"replies":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867\/posts\/2\/replies\/",
"likes":"https:\/\/public-api.wordpress.com\/rest\/v1.1\/sites\/89749867\/posts\/2\/likes\/"
}
},
"capabilities":{
"publish_post":false,
"delete_post":false,
"edit_post":false
}
}
]
}
这是我的课程(有一些属性被注释掉,所以我没有必要建立这些:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace thenickwilson_SPA_NEW.Models
{
public class Post
{
public int ID { get; set; }
public int site_ID { get; set; }
//public Author author { get; set; }
public string date { get; set; }
public string modified { get; set; }
public string title { get; set; }
public string URL { get; set; }
public string short_URL { get; set; }
public string content { get; set; }
public string excerpt { get; set; }
public string slug { get; set; }
public string guid { get; set; }
public string status { get; set; }
public bool sticky { get; set; }
public string password { get; set; }
public bool parent { get; set; }
public string type { get; set; }
//public Discussion discussion { get; set; }
public bool likes_enabled { get; set; }
public bool sharing_enabled { get; set; }
public int like_count { get; set; }
public int i_like { get; set; }
public int is_reblogged { get; set; }
public int is_following { get; set; }
public string global_ID { get; set; }
public string featured_image { get; set; }
//public PostThumbnail post_thumbnail { get; set; }
public string format { get; set; }
public bool geo { get; set; }
public int menu_order { get; set; }
public string page_template { get; set; }
public List<object> publicize_URLs { get; set; }
//public Tags tags { get; set; }
//public Categories categories { get; set; }
//public Attachments attachments { get; set; }
public int attachment_count { get; set; }
//public List<Metadata> metadata { get; set; }
//public Meta3 meta { get; set; }
//public Capabilities capabilities { get; set; }
}
}
这是我的其余代码:
[ResponseType(typeof(Post))]
public IHttpActionResult Get()
{
var client = new RestClient("http://public-api.wordpress.com/rest/v1.1/");
var request = new RestRequest("sites/{id}/posts", Method.GET);
request.AddUrlSegment("id", "myblog.wordpress.com");
request.RequestFormat = DataFormat.Json;
var response = client.Execute<List<Post>>(request);
return this.Ok(response);
}
我有一种感觉,它与API返回“找到”元素以及“帖子”的事实有关,但我不确定。任何帮助将不胜感激。
答案 0 :(得分:3)
您正在接收包装在容器对象中的数组或帖子列表,因此请尝试使用:
public class MyContainer
{
public List<Post> posts {get;set;}
}
和
var response = client.Execute<MyContainer>(request);
您可以在此处查看您的json反序列化:http://jsonviewer.stack.hu/