c# - 为什么 Web API 不反序列化这个但 JSON.Net 会反序列化?

标签 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 Controller :

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.
}

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

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

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

[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 Controller 。为什么这不起作用?

编辑: 根据要求,我正在使用此 Javascript(JQuery DataTables 的一部分)调用我的 Web API Controller 。请注意,我确信相同的 JSON 会到达我的 Controller ,因为我已经使用 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":""}}

最佳答案

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

关于c# - 为什么 Web API 不反序列化这个但 JSON.Net 会反序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12843972/

相关文章:

c# - 停止多线程 Windows 服务

c# - 在服务器上的 ASP.NET Web API 中下载文件请求返回 JSON 而不是文件

unit-testing - 使用 ODataQueryOptions 编写单元测试

c# - 强制泛型类型参数

c# - Metro 风格 WinRT XAML 应用中 SemanticZoom 中 ZoomedOutView 的奇怪定位

asp.net-mvc - ASP.NET MVC 页面不通过 3G 或某些代理服务器提供服务

javascript - 在 Ajax 调用后重定向到另一个页面?

asp.net-web-api - NSeviceBus 6 - 如何使用 Autofac 将 IMessageSession 注入(inject) .Net Web API Controller ?

c# - ASP.NET MVC3 Controller AOP 代理不拦截所有方法,只有 IController.Execute

asp.net-mvc - Web API Controller 和 MVC Controller - 身份验证