DataContractJsonSerializer generating Ghost string to JSON keys?

核能气质少年 提交于 2020-01-13 11:37:08

问题


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; }

Now following is the output generated

Output : Notice structure where only "name" is expected instead of k__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}

回答1:


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

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

C# automatic property deserialization of JSON



来源:https://stackoverflow.com/questions/2882059/datacontractjsonserializer-generating-ghost-string-to-json-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!