C# 使用Newtonsoft.Json的JsonProperty设置返回的Json数据列名/C# 通过实体类序列化生成自定义字段的json数据

原文链接:https://blog.csdn.net/weixin_44917045/article/details/103236167

                 https://blog.csdn.net/bazinga_y/article/details/134416680

在写分页的时候,返回Json数据给前台的时候,数据不能出来,原因就是Json数据的列名是大写的,而页面需要的是小写的。

 解决办法

1
2
3
4
5
6
7
8
public class PageResult<T>
{
    [JsonProperty("total")]
    public long Total { get; set; }
 
    [JsonProperty("rows")]
    public List<T> Rows { get; set; }
}

  

 

 

使用JsonProperty注解中的PropertyName 属性可以自定json字段名称,NullValueHandling 属性,为Include时,当该字段没有赋值时,生成json数据会包含该字段;当为Ignore时,该字段没有赋值时,生成json数据,会忽略该字段。
以下是实体示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
public class FundCollection
{
 
    /// <summary>
    /// 组织编码
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "pk_org", NullValueHandling = NullValueHandling.Include)]
    public string PkOrg { get; set; }
 
    /// <summary>
    /// 单据编号
    /// 是否必填:否
    /// </summary>
 
    [JsonProperty(PropertyName = "bill_no", NullValueHandling = NullValueHandling.Ignore)]
    public string BillNo { get; set; }
 
    /// <summary>
    /// 部门编码
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "pk_dept", NullValueHandling = NullValueHandling.Ignore)]
    public string PkDept { get; set; }
 
    /// <summary>
    /// 结算方式编码
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "pk_balatype", NullValueHandling = NullValueHandling.Include)]
    public string PkBalatype { get; set; }
 
    /// <summary>
    /// 单据日期
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "bill_date", NullValueHandling = NullValueHandling.Ignore)]
    public string BillDate { get; set; }
 
    /// <summary>
    /// 币种编码
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "pk_currtype", NullValueHandling = NullValueHandling.Ignore)]
    public string PkCurrtype { get; set; }
 
    /// <summary>
    /// 收款银行账户
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "pk_account", NullValueHandling = NullValueHandling.Include)]
    public string PkAccount { get; set; }
 
    /// <summary>
    /// 备注
    /// 收款时合并字段(供应商+费用类型)展示摘要中  
    /// 例如:收XX公司平台使用费
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "memo", NullValueHandling = NullValueHandling.Ignore)]
    public string Memo { get; set; }
 
    /// <summary>
    /// 自定义项1 银行流水编码
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "def1", NullValueHandling = NullValueHandling.Ignore)]
    public string Def1 { get; set; }
 
    /// <summary>
    /// 制单人用户编码
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "billmaker", NullValueHandling = NullValueHandling.Include)]
    public string Billmaker { get; set; }
 
    /// <summary>
    /// 来源系统编码
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "source_flag", NullValueHandling = NullValueHandling.Include)]
    public string SourceFlag { get; set; }
 
    /// <summary>
    /// 客户编码
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "pk_customer", NullValueHandling = NullValueHandling.Include)]
    public string PkCustomer { get; set; }
 
    /// <summary>
    /// json数组 传入多个对象
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "body", NullValueHandling = NullValueHandling.Include)]
    public List<FundCollectionBody> BodyList { get; set; }
 
}
 
public class FundCollectionBody
{
 
    /// <summary>
    /// 币种编码
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "pk_currtype", NullValueHandling = NullValueHandling.Ignore)]
    public string PkCurrtype { get; set; }
 
    /// <summary>
    /// 单据日期
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "bill_date", NullValueHandling = NullValueHandling.Ignore)]
    public string BillDate { get; set; }
 
    /// <summary>
    /// 收款原币金额
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "rec_primal", NullValueHandling = NullValueHandling.Include)]
    public string RecPrimal { get; set; }
 
    /// <summary>
    /// 税类别
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "def1", NullValueHandling = NullValueHandling.Ignore)]
    public string Def1 { get; set; }
 
    /// <summary>
    /// 贸易方式
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "def2", NullValueHandling = NullValueHandling.Ignore)]
    public string Def2 { get; set; }
 
    /// <summary>
    /// 税率
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "def3", NullValueHandling = NullValueHandling.Ignore)]
    public string Def3 { get; set; }
 
    /// <summary>
    /// 税金
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "def4", NullValueHandling = NullValueHandling.Ignore)]
    public string Def4 { get; set; }
 
    /// <summary>
    /// 创建时间
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "creationtime", NullValueHandling = NullValueHandling.Ignore)]
    public string Creationtime { get; set; }
 
    /// <summary>
    /// 付款银行账号 供应商账户名称
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "pk_oppaccount", NullValueHandling = NullValueHandling.Include)]
    public string PkOppaccount { get; set; }
 
    /// <summary>
    /// 客户编码
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "pk_customer", NullValueHandling = NullValueHandling.Ignore)]
    public string PkCustomer { get; set; }
 
    /// <summary>
    /// 收款银行账号
    /// 是否必填:是
    /// </summary>
    [JsonProperty(PropertyName = "pk_account", NullValueHandling = NullValueHandling.Include)]
    public string PkAccount { get; set; }
 
 
    /// <summary>
    /// 收款时合并字段(供应商+费用类型)展示摘要中  
    /// 例如:收XX公司平台使用费
    /// 是否必填:否
    /// </summary>
    [JsonProperty(PropertyName = "memo", NullValueHandling = NullValueHandling.Ignore)]
    public string Memo { get; set; }
}

  以下是调用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FundCollection fund = new FundCollection();
fund.BillNo = "PF-202301-00001";
fund.Billmaker = "10880D";
var list = new List<FundCollectionBody>();
FundCollectionBody body = new FundCollectionBody
{
    PkCurrtype = "类型",
    Memo = "备注标识"
};
list.Add(body);
fund.BodyList = list;
var serializerSettings = new JsonSerializerSettings
{
    // 设置为驼峰命名
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var jsonData= JsonConvert.SerializeObject(fund, (Newtonsoft.Json.Formatting)System.Xml.Formatting.None, serializerSettings);

  以下是生成的json数据:

1
{"pk_org":null,"bill_no":"PF-202301-00001","pk_balatype":null,"pk_account":null,"billmaker":"10880D","source_flag":null,"pk_customer":null,"body":[{"pk_currtype":"类型","rec_primal":null,"pk_oppaccount":null,"pk_account":null,"memo":"备注标识"}]}

  

posted @   yinghualeihenmei  阅读(248)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2023-06-06 SQL Server DATEADD() 函数
2022-06-06 BP神经网络的指纹识别系统
2022-06-06 压缩图片大小用电脑自带画图软件
点击右上角即可分享
微信分享提示