.net 反序列化时如何从 json 中删除 k__BackingField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13022198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:41:02  来源:igfitidea点击:

How to remove k__BackingField from json when Deserialize

.netjsonserializationdeserialization

提问by Filling The Stack is What I DO

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

在将 xml 文件序列化为 .net c# 对象后,我在返回的 json 中获取 k_BackingField。

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

我已将 DataContract 和 DataMember 属性添加到 .net c# 对象,但随后我在 json 客户端上一无所获。

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

返回的 json 示例:

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

采纳答案by jags

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

如果类可以用于序列化,则实际上不建议使用 Automatic 属性语法。原因是支持字段是由编译器生成的,每次编译代码时都可能不同。即使没有对类进行任何更改(仅重新编译代码),这也会导致不兼容问题。

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.

我认为在这种情况下应用 DataMember 属性将解决这个问题。但是我建议使用完整的属性语法,如果该类需要在序列化中使用。

回答by Safaa Elgendi

Remove [Serializable]from your class

[Serializable]从您的班级中删除

回答by Dan

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.

默认的 WebApi 序列化程序会将“__BackingField:”语法添加到 c# 自动属性中。将此添加到 App_Start 中的 WebConfig 以获得您可能正在寻找的更干净的 json。

using Newtonsoft.Json;
...

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

回答by Richard

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 IgnoreSerializableAttributeto truewill stop Newtonsoft.Json from behaving like Microsoft's serialisers and instead it will just serialise the public properties.

我们有一些对象被标记为[Serializable]可以使用传统方法序列化,但我们需要在 JSON 中完全序列化以用于 Web API。设置IgnoreSerializableAttributetrue将阻止 Newtonsoft.Json 表现得像 Microsoft 的序列化器,而只会序列化公共属性。

TLDR: Add this to WebApiConfig.cs:

TLDR:将此添加到 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.

主持人:与其删除一个已经被问过多次的问题的一个很好的答案,不如删除重复的问题。这是对有效问题的有效答案。

回答by Nagendra Upwanshi

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

简单易用且体面的数据公开方式我们需要将对象中的数据公开为易于阅读且一致的格式


First remove [Serializable]


首先删除[Serializable]

    [Serializable]

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

现在在类中添加[DataContract]并为属性添加[ DataMember],如下例所示

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

Hope this help
Thanks.

希望这有助于
谢谢。

回答by Jayaprakash Muthugopal

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

  1. [Serializable]从模型中删除

  2. 添加[DataContract][DataMember]到您的模型中 [Serializable]

  3. 将下面的行添加到 App_Start/WebApiConfig.cs

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

回答by sarh

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

在 JSON.NET 的情况下可能有帮助的另一种解决方案。使用 [Newtonsoft.Json.JsonObject] 属性标记类可能就足够了。

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.

我正在使用从 xsd 构建的 cs 类,并使用部分类添加一些属性。在 json 序列化之后,这些属性被标记为 k_BackingField。其他答案中提到的 JsonFormatter 设置也有帮助,但更简单的是用 [JsonObject] 属性标记部分类。

回答by Little Endian

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

我正在使用DataContractJsonSerializer另一个具有该Serializable属性的程序集的类。输出包含“k__BackingField”。删除Serializable属性(在另一个程序集中)修复了这个问题。不知道为什么。

回答by Teoman shipahi

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.

结果就有了,这个人与自己成为了朋友。我只是确保我的结果集中没有自引用对象。希望这可以帮助。

回答by JanBorup

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

我不得不使用 [Serializable] 属性,所以删除它不是一个选项。

XmlSerializer ignores [XmlAttribute] in WebApi

XmlSerializer 忽略 WebApi 中的 [XmlAttribute]

The above resolution solved it for me.

上述决议为我解决了它。

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