Newtonsoft JSON for .net is ignoring jsonproperty tags

前端 未结 1 1050
旧巷少年郎
旧巷少年郎 2020-12-11 06:49

For some really irritating reason, the JsonProperty tags are not working with Newtonsoft\'s Json for .net tool. In my class I have these:

    [JsonProperty(P         


        
1条回答
  •  情歌与酒
    2020-12-11 07:35

    Are you sure you're actually serializing using Json.Net? Json(MyClass) is an ASP.NET MVC method. MVC uses the JavaScriptSerializer class, which does not support [JsonProperty] attributes. To use the attributes, you would need to serialize using the Json.Net method JsonConvert.SerializeObject(MyClass). If you want to return that JSON from within an MVC controller then you would need call Content(jsonString, "application/json") instead of Json().

    0 讨论(0)
提交回复
热议问题