How to remove k__BackingField from json when Deserialize

.NetJsonSerializationDeserialization

.Net Problem Overview


I am getting the k_BackingField in my returned json after serializing a xml file to a .net c# object.

I've added the DataContract and the DataMember attribute to the .net c# object but then I get nothing on the json, client end.

[XmlRoot("person")]
[Serializable]
public class LinkedIn
{
    [XmlElement("id")]
    public string ID { get; set; }
        
    [XmlElement("industry")]
    public string Industry { get; set; }
        
    [XmlElement("first-name")]
    public string FirstName { get; set; }
        
    [XmlElement("last-name")]
    public string LastName { get; set; }
    [XmlElement("headline")]
}

Example of the returned json:

home: Object
<FirstName>k__BackingField: "Storefront"
<LastName>k__BackingField: "Doors"

.Net Solutions


Solution 1 - .Net

Remove [Serializable] from your class

Solution 2 - .Net

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 - .Net

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 - .Net

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.

Solution 5 - .Net

Simple Easy and Decent way to expose data We need to expose out data in object to easy readable and consistent format


First remove [Serializable]

    [Serializable]

now add [DataContract] in class and [DataMember] for property like below example

[DataContract]
public class UserDiscretion : UserReport
{
    [DataMember]
    public String DiscretionCode { get; set; }
    public String DiscretionDescription { get; set; }
}

Hope this help
Thanks.

Solution 6 - .Net

Couple of options:

  1. Remove [Serializable] from model

  2. Add [DataContract] and [DataMember] to your model along with [Serializable]

  3. Add below line to App_Start/WebApiConfig.cs

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

Solution 7 - .Net

Another solution that may help in case of JSON.NET. It may be enough to mark class with [Newtonsoft.Json.JsonObject] attribute.

I was working with cs classes built from xsd and was adding some properties using partial classes. After json serialization these properties were marked with k_BackingField. JsonFormatter settings mentioned in other answers helped as well, but more simple was to mark partial class with [JsonObject] attribute.

Solution 8 - .Net

I was using DataContractJsonSerializer with a class from another assembly that had the Serializable attribute. The output contained "k__BackingField". Removing the Serializable attribute (in the other assembly) fixed this. Not sure why.

Solution 9 - .Net

Assuming you see this issue inside of your MVC project, I've found that it's pretty simple to replace the use of @Html.JsonData. Here is a snippet of code that has worked for me in the past:

<input type="hidden" id="Model" value="@Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model))" />

Not as elegant, but simple in a pinch.

Solution 10 - .Net

I had this issue when I have self reference properties in my class such as;

class Person {
 List<Person> Friends { get; set;}
}

And there was a result, the person was friend with himself. I just made sure there was no self referencing objects in my result set. Hope this helps.

Solution 11 - .Net

I had to use the [Serializable] attributes, so removing it was not an option.

https://stackoverflow.com/questions/35846873/xmlserializer-ignores-xmlattribute-in-webapi

The above resolution solved it for me.

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

Solution 12 - .Net

in my case this error was for the Newtonsoft.Json Version, the server looked for 6.0.0 version and I had the 11.0, so I had to install the version 6.0.0

Solution 13 - .Net

Friends, don't declare properties like this:

public String DiscretionCode { get; set; }
public String DiscretionDescription { get; set; }

But, create auxiliar vars, like old....

private String discretionCode;

public String DiscretionCode 
{ 
    get { return discretionCode;}
    set { discretionCode = value; }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFilling The Stack is What I DOView Question on Stackoverflow
Solution 1 - .NetSafaa ElgendiView Answer on Stackoverflow
Solution 2 - .NetDanView Answer on Stackoverflow
Solution 3 - .NetjagsView Answer on Stackoverflow
Solution 4 - .NetRichardView Answer on Stackoverflow
Solution 5 - .NetNagendra UpwanshiView Answer on Stackoverflow
Solution 6 - .NetJayaprakash MuthugopalView Answer on Stackoverflow
Solution 7 - .NetsarhView Answer on Stackoverflow
Solution 8 - .NetLittle EndianView Answer on Stackoverflow
Solution 9 - .NetRyan RoarkView Answer on Stackoverflow
Solution 10 - .NetTeoman shipahiView Answer on Stackoverflow
Solution 11 - .NetJanBorupView Answer on Stackoverflow
Solution 12 - .NetAndres GuillenView Answer on Stackoverflow
Solution 13 - .NetHumberto Gonçalves de AlmeidaView Answer on Stackoverflow