如何从JSON反序列化时,删除k__BackingField [英] How to remove k__BackingField from json when Deserialize

查看:735
本文介绍了如何从JSON反序列化时,删除k__BackingField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是序列化XML文件到.NET C#对象之后得到k_BackingField在我返回的JSON。

我已经添加了DataContract和数据成员属性的.NET C#对象,但当时我什么也得不到的JSON,客户端。

  [XmlRoot(人)]
[可序列化]
公共类LinkedIn
{
    [的XmlElement(ID)
    公共字符串ID {获得;组; }

    [的XmlElement(工业)
    公共字符串行业{获得;组; }

    [的XmlElement(直呼其名)]
    公共字符串名字{获得;组; }

    [的XmlElement(最后的名字)]
    公共字符串名字{获得;组; }
    [的XmlElement(标题)
}
 

返回的JSON的示例:

家:对象 <名字> k__BackingField:店面 <名字> k__BackingField:门

解决方案

自动属性语法实际上是不建议,如果该类可以序列化使用。由编译器可以是不同的,每次code编译产生的原因是支持字段。这可能会导致不兼容问题,即使是没有改变的类(只是重新编译code)。

我想将数据成员属性将在这种情况下,解决该问题。不过,我会建议使用全属性的语法,如果类需要在序列中使用。

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"

解决方案

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.

这篇关于如何从JSON反序列化时,删除k__BackingField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆