簡體   English   中英

JsonConvert.DeserializeObject <T> (值)不反序列化分配的對象

[英]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 :(&nbsp;</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時,我得到以下信息: 使用VS內置的可視化工具

我使用此BlogPost vari = JsonConvert.DeserializeObject<BlogPost>(value); 反序列化我的對象,但一切都保持為

調試我的vari對象

我的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM