How to remove k__BackingField from json when Deserialize

I get k_BackingField in my returned json after serializing the xml file into a C # .net object.

I added the DataContract and DataMember attribute to the Cnet .net object, but then I get nothing on the json side, the client.

[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 returned json:

 home: Object <FirstName>k__BackingField: "Storefront" <LastName>k__BackingField: "Doors" 
+93
json serialization deserialization
Oct. 23
source share
13 answers

The syntax of automatic syntax is not really recommended if the class can be used in serialization. The reason underlying the support field is generated by the compiler, which may be different each time the code is compiled. This can cause incompatibility problems even if no changes have been made to the class (just recompile the code).

I think that applying the DataMember attribute will fix the problem in this case. But I would recommend using the full property syntax if the class should be used in serialization.

+43
Oct 23 '12 at 1:27
source share

Remove [Serializable] from your class

+96
Jan 06 '15 at 10:09
source share

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

 using Newtonsoft.Json; ... config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings(); 
+48
Feb 26 '16 at 19:11
source share

We have some objects that are marked as [Serializable] , so they can be serialized using traditional methods, but we must have purely serialized in JSON for use with the Web API. Setting IgnoreSerializableAttribute to true will stop Newtonsoft.Json from behaving like Microsoft serializers, and instead it just serializes the public properties.

TL; DR: add this to WebApiConfig.cs:

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

Moderator: Instead of deleting a really good answer to a question that was asked several times, delete the duplicate question. This is a valid answer to the right question.

+35
Apr 28 '16 at 9:24
source share

A simple and decent way to publish data. We need to expose the data in the object in an easily readable and consistent format


Uninstall [Serializable] first

  [Serializable] 

now add [DataContract] to the class and [DataMember] for the property as shown below.

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

Hope this help
Thank.

+9
Feb 08 '16 at 9:26
source share

A couple of options:

  • Remove [Serializable] from the model

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

  • Add the line below App_Start/WebApiConfig.cs

 config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings(); 
+5
May 01 '17 at 15:51
source share

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

I worked with cs classes built from xsd and added some properties using partial classes. After json serialization, these properties were marked with k_BackingField. The JsonFormatter settings mentioned in other answers also helped, but it was simpler to mention a partial class with the [JsonObject] attribute.

+3
Jul 27 '16 at 16:50
source share

I used a DataContractJsonSerializer with a class from another assembly that had a Serializable attribute. The output contained "k__BackingField". Fixed removal of the Serializable attribute (in another assembly). I do not know why.

+2
Dec 11 '14 at 0:07
source share

Assuming you see this problem inside your MVC project, I have found that it is quite simple to replace using @ Html.JsonData. Here is a snippet of code that worked for me in the past:

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

Not so elegant, but just as a last resort.

0
Feb 12 '14 at 18:26
source share

I had this problem when I have my own link properties in my class, such as:

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

And there was a result, a man was a friend with himself. I just made sure that there were no self-regulatory objects in my result set. Hope this helps.

0
Oct. 19 '17 at 19:21
source share

I had to use the [Serializable] attributes, so it was impossible to remove it.

XmlSerializer ignores [XmlAttribute] in WebApi

The above solution solved this for me.

 GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true; 
0
Oct 24 '18 at 7:15
source share

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

0
Aug 21 '19 at 20:38
source share

Friends, do not declare properties as follows:

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

But create auxiliary vars like the old ones ....

 private String discretionCode; public String DiscretionCode { get { return discretionCode;} set { discretionCode = value; } } 
-2
Feb 11 '16 at 12:18
source share



All Articles