Newtonsoft JSON for .net is ignoring jsonproperty tags(.net 的 Newtonsoft JSON 忽略 jsonproperty 标签)
问题描述
出于一些真正令人恼火的原因,JsonProperty 标签无法与 Newtonsoft 的 Json for .net 工具一起使用.在我的课堂上,我有这些:
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(PropertyName = "id")]
public string ID { get; set; }
[JsonProperty(PropertyName = "title")]
public string Title { get; set; }
[JsonProperty(PropertyName = "url")]
public string Url { get; set; }
[JsonProperty(PropertyName = "class")]
public string EventClass { get; set; }
[JsonProperty(PropertyName = "start")]
public string Start { get; set; }
[JsonProperty(PropertyName = "end")]
public string End { get; set; }
但是我收到了这个
{"success":true,
"result": [{
"ID":"0",
"Title":"Eid ul-Fitr",
"Url":"<blah>",
"EventClass":"event-info",
"Start":"1406520000000",
"End":"1406606400000"},
etc.
如您所见,它忽略了我设置属性名称.我也尝试过使用 [System.Runtime.Serialization.DataMember(Name="id")] 但没有奏效.
As you can see it is ignoring me setting the property name. I have tried using [System.Runtime.Serialization.DataMember(Name="id")] as well and that has not worked.
这才是真正让我陷入困境的原因.它昨天奏效了.我把它回滚到昨晚我提交时的位置,但它仍然无法工作.
Here is what is really driving me up the wall. It worked yesterday. I rolled it back to where it was last night when I committed and it still won't work.
有什么想法吗?
推荐答案
您确定您实际上是在使用 Json.Net 进行序列化吗?Json(MyClass) 是一种 ASP.NET MVC 方法.MVC 使用 JavaScriptSerializer 类,该类不支持 [JsonProperty] 属性.要使用这些属性,您需要使用 Json.Net 方法 JsonConvert.SerializeObject(MyClass) 进行序列化.如果您想从 MVC 控制器中返回该 JSON,则需要调用 Content(jsonString, "application/json") 而不是 Json().
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().
这篇关于.net 的 Newtonsoft JSON 忽略 jsonproperty 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.net 的 Newtonsoft JSON 忽略 jsonproperty 标签
基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
