【Newtonsoft.Json】json序列化小驼峰格式(属性名首字母小写)

我是一名 ASP.NET 程序员,专注于 B/S 项目开发。累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html

只需要设置JsonSerializerSettings参数即可。

代码如下:

public class TestModel
        {
            public string Name { get; set; }

            public string Age { get; set; }

            public string Url { get; set; } = "https://www.itsvse.com";

            public int CustomId { get; set; }
        }
        static void Main(string[] args)
        {
            var model = new TestModel()
            {
                Name = "架构师",
                Age = "5",
                CustomId = 10086
            };
            var setting = new JsonSerializerSettings
            {
                ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            };
            var json = JsonConvert.SerializeObject(model, setting);
            Console.WriteLine(json);
            var json1 = JsonConvert.SerializeObject(model);
            Console.WriteLine(json1);
            Console.Read();
        }

效果图如下:

{"name":"架构师","age":"5","url":"https://www.itsvse.com","customId":10086}
{"Name":"架构师","Age":"5","Url":"https://www.itsvse.com","CustomId":10086}

Newtonsoft.Json

原文地址:https://down.itsvse.com/k/m4fdvk.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用如下代码实现: ``` using Newtonsoft.Json; using System.Collections.Generic; public class MyDictionaryConverter : JsonConverter { public override bool CanConvert(Type objectType) { return objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(Dictionary<,>); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var dictionary = (IDictionary)value; var keyType = dictionary.GetType().GetGenericArguments()[0]; var valueType = dictionary.GetType().GetGenericArguments()[1]; writer.WriteStartObject(); foreach (var key in dictionary.Keys) { writer.WritePropertyName(JsonConvert.SerializeObject(key)); serializer.Serialize(writer, dictionary[key]); } writer.WriteEndObject(); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var dictionary = (IDictionary)Activator.CreateInstance(objectType); var keyType = objectType.GetGenericArguments()[0]; var valueType = objectType.GetGenericArguments()[1]; while (reader.Read() && reader.TokenType == JsonToken.PropertyName) { var propertyName = JsonConvert.DeserializeObject(reader.Value.ToString(), keyType); var value = serializer.Deserialize(reader, valueType); dictionary.Add(propertyName, value); } return dictionary; } } ``` 使用方法如下: ``` // 序列化 Dictionary<string, object> dic = new Dictionary<string, object>(); string json = JsonConvert.SerializeObject(dic, new MyDictionaryConverter()); // 反序列化 Dictionary<string, object> result = JsonConvert.DeserializeObject<Dictionary<string, object>>(json, new MyDictionaryConverter()); ``` 需要注意的是,需要在属性上添加 `[JsonConverter(typeof(MyDictionaryConverter))]` 特性来告诉 `Newtonsoft.Json` 使用上述自定义的 `MyDictionaryConverter` 来进行序列化和反序列化

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值