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?
JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.
Yet Newtonsoft. Json was basically scrapped by Microsoft with the coming of . NET Core 3.0 in favor of its newer offering designed for better performance, System. Text.
Per the documentation: this is the json key that will be used when serializing/deserializing this object to/from a json string. So, if the value of FileName is file.txt , the serialized result would be { "fileName": "file.txt" }
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With