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数据的列名是大写的,而页面需要的是小写的。

 解决办法

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数据,会忽略该字段。
以下是实体示例:

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

  以下是调用示例:

  			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数据:

{"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 @ 2024-06-06 17:53  yinghualeihenmei  阅读(24)  评论(0编辑  收藏  举报