How to remove k__BackingField from json when Deserialize

Solution 1:

Remove [Serializable] from your class

Solution 2:

The default WebApi serializer will add that "__BackingField:" syntax to c# auto-properties. Add this to your WebConfig in App_Start to get the cleaner looking json that you might be looking for.

using Newtonsoft.Json;
...

config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings();

Solution 3:

Automatic Property syntax is actually not recommended if the class can be used in serialization. Reason being the backing field is generated by compiler which can be different each time code is compiled. This can cause incompatibility issues even if no change is made to the class (just recompiling the code).

I think applying DataMember attribute will fix the issue in this case. But I would recommend to use full property syntax, if the class needs to be used in serialization.

Solution 4:

We have some objects which are marked as [Serializable] so they can be serialised using traditional methods, but which we need to have cleanly serialised in JSON for use with Web API. Setting IgnoreSerializableAttribute to true will stop Newtonsoft.Json from behaving like Microsoft's serialisers and instead it will just serialise the public properties.

TLDR: Add this to WebApiConfig.cs:

((Newtonsoft.Json.Serialization.DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;

Moderator: Rather than deleting a really good answer to a question that has been asked several times, please delete the duplicate question. This is a valid answer to a valid question.