[英]JsonConvert.DeserializeObject does not deserialize my json
[英]JsonConvert.DeserializeObject<T>(value) does not deserialize the object assigned
我創建了一個帶有KnockoutJS視圖模型的頁面。 我想使用Web API將數據發布到我的服務器。
我使用此AJAX帖子:
$.ajax({
url: "/api/blogpost",
contenttype: "application/x-www-form-urlencoded",
data: '=' + encodeURIComponent(ko.toJSON(self.Blog)),
type: "POST",
dataType: "JSON",
timeout: 10000,
success: function (Result) {
},
error: function (xhr, status) {
alert(status + " - " + xhr.responseText);
}
});
要將JSON數據發送到我的Web API方法。 這是發送到服務器的JSON:
{
"BlogTitle": "Sample Post",
"BlogHTML": "<p><strong>Sample JSON Blog Post</strong></p>\n\n<h1><strong>It never works :( </strong></h1>\n",
"BlogThumbnail": "http://mysystemURL/SamplePost/What.jpg",
"BlogSummary": "This is a sample post",
"BlogFQURL": "Sample_Post",
"BlogTags": [
"json",
"devlopment",
"newtag",
""
],
"BlogCategory": 1
}
我的WEB API方法正確接收了JSON數據。 RAW字符串值如下所示:
"{\"BlogTitle\":\"Sample Post\",\"BlogHTML\":\"<p><strong>Sample JSON Blog Post</strong></p>\\n\\n<h1><strong>It never Works :(</strong></h1>\\n\",\"BlogThumbnail\":\"http://mysystemURL/SamplePost/What.jpg\",\"BlogSummary\":\"This is a sample post\",\"BlogFQURL\":\"Sample_Post\",\"BlogTags\":\"[\\\"json\\\",\\\"devlopment\\\",\\\"newtag\\\",\\\"\\\"]\",\"BlogCategory\":1}"
當我在數據上使用JSON visulizer時,我得到以下信息:
我使用此BlogPost vari = JsonConvert.DeserializeObject<BlogPost>(value);
反序列化我的對象,但一切都保持為空
我的BlogPost對象看起來像這樣:
public class BlogPost
{
public int BlogPostID { get; set; }
public string BlogPostTitle { get; set; }
public string BlogPostHTML { get; set; }
public string BlogPostThumbnailURL { get; set; }
public string BlogPostSummary { get; set; }
public string BlogPostFQURL { get; set; }
public int BlogPostCategory { get; set; }
public List<TagDTO> BlogPostTags { get; set; }
}
我真的很沮喪。任何幫助將不勝感激!
您的媒體資源名稱不匹配。 C#對象的屬性是BlogPost*
,而JSON具有Blog*
,而沒有Post 。
在Javascript或C#端更正名稱,或使用JsonProperty
屬性指定序列化屬性的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.