Change the way JSON.NET serializes property names

。_饼干妹妹 提交于 2019-12-11 00:25:18

问题


How can I change the way Newtonsoft JSON.NET serializes property names of objects?


回答1:


A couple of ways:

  1. You can manually control how it serializes using the JsonTextWriter class:
    • http://james.newtonking.com/projects/json/help/index.html?topic=html/ReadingWritingJSON.htm
  2. You could implement a custom JsonConverter that does what you want:
    • http://james.newtonking.com/projects/json/help/index.html?topic=html/T_Newtonsoft_Json_JsonConverter.htm
    • Example: JSON.NET customizing the serialization to exclude a property name



回答2:


You can create a model with the property names. And change them by creating some private variables that will be use to as return values for the properties. This is will direct the deserializer to reset the name of the property.

    private int _privateId;

    public int NameThatExistAlreadyInTheJson 
    {

        set { _privateId = value; }

    }
    public int NameYouWantItToBeDisplayInstead 
    {
        get { return _privateId; }
    }


来源:https://stackoverflow.com/questions/13091862/change-the-way-json-net-serializes-property-names

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