Newtonsoft JSON for .net is ignoring jsonproperty tags

10,030

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().

Share:
10,030
Admin
Author by

Admin

Updated on June 13, 2022

Comments

  • Admin
    Admin almost 2 years

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

    But I am receiving this

    {"success":true,
     "result": [{
        "ID":"0",
        "Title":"Eid ul-Fitr",
        "Url":"<blah>",
        "EventClass":"event-info",
        "Start":"1406520000000",
        "End":"1406606400000"},
      etc.
    

    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.

    Any thoughts?