为什么Web API不会反序列化,但JSON.Net会?

时间:2012-10-11 16:11:03

标签: c# asp.net-mvc asp.net-web-api json.net

Web API如何无法反序列化JSON.Net反序列化的对象?

Visual Studio showing Web API's attempt as all nulls but JSON.Net's properly populated

这是Web API控制器:

public void Put(EditorSubmissionMainView ajaxSubmission) {
// ajaxSubmission: EditorSubmissionMainView with all values ('data' also == null)

    string json = "{\"id\":\"row_1377\",\"data\":{\"ROTATION\":\"1\",\"EQUIPMENT\":[{\"id\":\"6\"},{\"id\":\"8\"}],\"NOTES\":\"\"}}";

    EditorSubmissionMainView foo = Newtonsoft.Json.JsonConvert.DeserializeObject<EditorSubmissionMainView>(json) as EditorSubmissionMainView;
// foo is a EditorSubmissionMainView but properly deserialized.
}

这是JSON,由Fiddler捕获并格式化:

{
    "id": "row_1377",
    "data": {
        "ROTATION": "1",
        "EQUIPMENT": [{
            "id": "6"
        },
        {
            "id": "8"
        }],
        "NOTES": ""
    }
}

使用JSON.Net序列化但不使用Web API控制器序列化的示例类:

[Serializable]
public class EditorSubmissionMainView
{
    public string id { get; set; }
    public EditorSubmissionMainViewData data { get; set; }
}

[Serializable]
public class EditorSubmissionMainViewData
{
    [JsonProperty("ROTATION")]
    public int? rotation { get; set; } // Same problem if everything is a string

    [JsonProperty("EQUIPMENT")]
    public ICollection<Dictionary<string, int?>> equipment { get; set; }

    [JsonProperty("NOTES")]
    public string notes { get; set; }
}

Web API使用JSON.Net,我没有使用任何自定义JSON格式化程序 - 只是将JSON传递给Web API控制器。为什么这不起作用?

编辑: 根据要求,我使用此Javascript(JQuery DataTables的一部分)调用我的Web API控制器。请注意,我确信相同的JSON会进入我的控制器,因为我已经使用Fiddler捕获了原始HTTP数据包,并确保它是正确的:

"ajaxUrl": {
    "create": "POST @Url.Content("~/API/MainView")",
    "edit":   "PUT @Url.Content("~/API/MainView")",
    "remove": "DELETE @Url.Content("~/API/MainView")"
},

"ajax": function (method, url, data, successCallback, errorCallback) {
    $.ajax({
        "type": method,
        "url": url,
        "data": JSON.stringify(data), // Requires IE8+
        "contentType": "application/json",
        "dataType": "json",
        "success": function (json) {
            successCallback(json);
        },
        "error": function (xhr, error, thrown) {
            errorCallback(xhr, error, thrown);
        }
    });
},

原始HTTP请求如下:

PUT http://localhost:53367/API/MainView HTTP/1.1
x-requested-with: XMLHttpRequest
Accept-Language: en-us
Referer: http://localhost:53367/Manage/MainView
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost:53367
Content-Length: 306
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=wqsghjrol20cszrxfzdm0qo4

{"id":"row_1377","data":{"ROTATION":"1","EQUIPMENT":[{"id":"6"},{"id":"8"}],"NOTES":""}}

2 个答案:

答案 0 :(得分:22)

尝试从您的类EditorSubmissionMainView和EditorSubmissionMainViewData中删除[Serializable]属性。

答案 1 :(得分:0)

我认为问题可能是在你的json中使用“data”或“id”。因为这必须通过模型绑定器,它可能会被默认路由中的“id”变量搞糊涂。或者,它可能会被通常用于描述有效载荷本身的通用术语“数据”所迷惑。

尝试更改这些名称,如果有解决方法,请告诉我们。