.net的Newtonsoft JSON忽略了jsonproperty标签 [英] Newtonsoft JSON for .net is ignoring jsonproperty tags
问题描述
出于某些令人烦恼的原因,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标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!