序列化Json时遇到的大小写问题及解决方法

  最近在一个webapi2项目中遇到了一个问题:C#编码规范中规定属性的首字母是大写的(大多数公司采用这种编码风格),但是从其它系统中接收到的json对象的属性却是小写的(大多数公司采用这种编码风格),怎样才能方便地完成转换?

  最粗暴的方法是定义一个所有属性名为小写的ViewModel类,然后再编写相应的方法来完成大小写属性的转换。

      有没有更加优雅的做法?

  在webapi2,默认的json序列化组件是Newtonsoft.Json。怎样才能让Newtonsoft.Json在序列化的时候自动将大写转换成小写,而在反序列化的时候自动转换成大写?查找文档之后,发现使用JsonPropertyAtrribute即可。要使用JsonPropertyAtrribute,需要先引用命名空间Newtonsoft.Json。如下所示:

 1 using Newtonsoft.Json;
 2 
 3   /// <summary>
 4   ///商品类。    
 5   /// </summary>
 6     public class Product
 7     {
 8         /// <summary>
 9         /// 商品Id
10         /// </summary>
11         [JsonProperty("productId")]
12         public string ProductId { set; get; }
13 
14 
15         /// <summary>
16         /// 商品名称
17         /// </summary>
18         [JsonProperty("productName")]
19         public string ProductName { set; get; }
20 }

在代码中,我们使用[JsonProperty("productId ")]来修饰ProductId属性,productId为小写,ProductId为大写。在序列化和反序列的时候,Newtonsoft.Json会自动进行转换。

原文地址:https://www.cnblogs.com/dehai/p/4581664.html