DataContractJsonSerializer生成Ghost字符串到JSON密钥? [英] DataContractJsonSerializer generating Ghost string to JSON keys?

查看:97
本文介绍了DataContractJsonSerializer生成Ghost字符串到JSON密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataContractJsonSerializer 这是.net框架中添加的不错的类可以用于将对象序列化/反序列化为JSON.

DataContractJsonSerializer this is nice class added in the .net framework which can be used to serialize/desirealize object into JSON.

现在,以下是我正在尝试的示例

Now following is the example i am trying

[Serializable] class User { public string name;     public string userId; }

现在是生成的输出

输出:通知结构,其中仅应使用名称",而不是k__BackingField

Output : Notice structure where only "name" is expected instead of k__BackingField

现在这是经过大量挖掘后的问题,我不确定<>和_BackingField将从何处来?

Now this is the problem after digging so much i am not sure from where <> and _BackingField is coming ?

{
"<name>k__BackingField":"test user",
"<userId>k__BackingField":100001}

推荐答案

这只是有根据的猜测.我认为这是因为您使用的是公共字段,而不是名称和用户ID的属性.

This is just an educated guess. I think it's because you're using public fields instead of properties for name and userid.

看来这也与您使用[Serializable]属性而不是[DataContract]和[DataMember]有关.请查看此帖子以获取更多详细信息:

It appears it also has to do with the fact that you are using the [Serializable] attribute instead of [DataContract] and [DataMember]. Check out this post for more detail:

JSON的C#自动属性反序列化

这篇关于DataContractJsonSerializer生成Ghost字符串到JSON密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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