Changing property names for serializing

后端 未结 1 1039
时光说笑
时光说笑 2020-12-11 01:29

My class has a property \'PropertyA\', I want this to appear as \'PropertyB\' in a JSON object when it\'s serialized. Is there any sort of attribute I can use?

相关标签:
1条回答
  • 2020-12-11 01:44

    For Json.NET and DataContractJsonSerializer use DataMemberAttribute:

    [DataMember(Name="PropertyB")]
    T PropertyA { ... }
    

    Make sure that your class is decorated with the [DataContract] attribute as well.

    If you're using JavaScriptSerializer, you need to create derived implementation, as described here: JavaScriptSerializer.Deserialize - how to change field names

    0 讨论(0)
提交回复
热议问题