C# 序列化Json时如何忽略JsonProperty(PropertyName =“ someName”)

前言

  序列化大家都很常见,就是把一个对象序列化成一串Json 字符串。最近对接第三方的时候遇到了一个情况,我们C#都是用骆驼命名,而他们呢需要接收的Json字符串的 key 是 小写 的这时候要怎么处理呢!话不多说直接上代码开干

定义一个 Map 

using Newtonsoft.Json;
using System;

namespace JsonLower
{
    public class Map
    {
        [JsonProperty("id")]
        public Guid ID { get; set; }
        [JsonProperty("code")]
        public string Code { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
    }
}

主程序初始化数据转Json字符串

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace JsonLower
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Map> maps = new List<Map>()
            {
                new Map{
                    ID = Guid.NewGuid(),
                    Code = "MD0001",
                    Name = "小明"
                },
                new Map{
                    ID = Guid.NewGuid(),
                    Code = "MD0002",
                    Name = "小红"
                },
                new Map{
                    ID = Guid.NewGuid(),
                    Code = "MD0003",
                    Name = "张三"
                },
            };


            var jsonStr = JsonConvert.SerializeObject(maps);
            Console.WriteLine(jsonStr);
            Console.ReadLine();
        }
    }
}

看下效果

C# 序列化Json时如何忽略JsonProperty(PropertyName =“ someName”)

在属性上使用了 JsonProperty 特性,使得序列化时根据 JsonProperty 特性传入的字符串进行序列化,那我们有时候想忽略掉 JsonProperty 特性

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonLower
{
    /// <summary>
    /// Json 序列化
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// 序列化是否按照JsonProperty显示Key
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="isJsonProperty"></param>
        /// <returns></returns>
        public static string SerializeObject(object obj, bool isJsonProperty = false)
        {
            var jsonSettings = new JsonSerializerSettings
            {
                DefaultValueHandling = DefaultValueHandling.Ignore
            };
            if (isJsonProperty)
                jsonSettings.ContractResolver = new LongNameContractResolver();
            return JsonConvert.SerializeObject(obj, jsonSettings);
        }
    }

    public class LongNameContractResolver : DefaultContractResolver
    {
        protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        {
            IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
            foreach (JsonProperty prop in list)
            {
                prop.PropertyName = prop.UnderlyingName;
            }
            return list;
        }
    }
}

 修改主程序代码

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace JsonLower
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Map> maps = new List<Map>()
            {
                new Map{
                    ID = Guid.NewGuid(),
                    Code = "MD0001",
                    Name = "小明"
                },
                new Map{
                    ID = Guid.NewGuid(),
                    Code = "MD0002",
                    Name = "小红"
                },
                new Map{
                    ID = Guid.NewGuid(),
                    Code = "MD0003",
                    Name = "张三"
                },
            };

            //未忽略
            {
                var jsonStr = JsonConvert.SerializeObject(maps);
                Console.WriteLine("未忽略JsonProperty:" + jsonStr);
            }
            {
                var jsonStr = JsonHelper.SerializeObject(maps,true);
                Console.WriteLine("忽略JsonProperty:" + jsonStr);
            }

            Console.ReadLine();
        }
    }
}

 看一下效果图

C# 序列化Json时如何忽略JsonProperty(PropertyName =“ someName”)

 

 

大功告成!

参考文章:https://www.it1352.com/1504360.html

上一篇:SpringBoot解决驼峰命名 ---返回Json实体类属性大小写问题


下一篇:JSON和GSON