.net - 如何让 Newtonsoft.Json 以我想要的方式序列化属性名称

标签 .net json.net .net-5

我试图以这样的 JSON 结尾:

{"KIBANA_INDEX":"whatever"}

但是当我尝试像这样使用 JsonPropertyAttribute 时:

[JsonProperty(PropertyName ="KIBANA_INDEX")]
public string KibanaIndex{ get; set; }

我最终得到的是这样的 JSON:

{"kibanA_INDEX":"whatever"}

有什么方法可以让 Newtonsoft.Json 屈服于我的意志吗?

最佳答案

默认情况下,Json.Net 不会那样做。如果您在 [JsonProperty] 属性中提供特定名称,序列化程序将遵守它,您应该会在输出中看到它。下面是一个示例程序来演示:

using System;
using Newtonsoft.Json;
                    
public class Program
{
    public static void Main()
    {
        var foo = new Foo { KibanaIndex = "whatever" };
        var json = JsonConvert.SerializeObject(foo);
        Console.WriteLine(json);
    }
}

public class Foo
{
    [JsonProperty(PropertyName = "KIBANA_INDEX")]
    public string KibanaIndex { get; set; } 
}

输出:

{"KIBANA_INDEX":"whatever"}

在这里 fiddle :https://dotnetfiddle.net/N753GP

我怀疑您实际上使用的是 CamelCasePropertyNamesContractResolver .此解析器将导致所有属性名称输出为驼峰式大小写,包括您已通过 [JsonProperty] 属性为其指定名称的属性名称。这里又是同一个例子,只是改为使用 CamelCasePropertyNamesContractResolver:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
                    
public class Program
{
    public static void Main()
    {
        var foo = new Foo { KibanaIndex = "whatever" };
        var resolver = new CamelCasePropertyNamesContractResolver();
        var settings = new JsonSerializerSettings { ContractResolver = resolver };
        var json = JsonConvert.SerializeObject(foo, settings);
        Console.WriteLine(json);
    }
}

这是输出,看起来应该很熟悉:

{"kibanA_INDEX":"whatever"}

fiddle :https://dotnetfiddle.net/KBhreA

如果这不是您想要的行为,很容易改变。为此,您只需要设置 OverrideSpecifiedNames解析器中 NamingStrategy 的属性设置为 false,如下所示。 (请注意,我在此示例中向 Foo 类添加了另一个属性,以表明驼峰式外壳仍然适用于 具有 [JsonProperty] 的属性 属性。)

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
                    
public class Program
{
    public static void Main()
    {
        var foo = new Foo { KibanaIndex = "whatever", AnotherProperty = "whatever" };
        var resolver = new CamelCasePropertyNamesContractResolver();
        resolver.NamingStrategy.OverrideSpecifiedNames = false;
        var settings = new JsonSerializerSettings { ContractResolver = resolver };
        var json = JsonConvert.SerializeObject(foo, settings);
        Console.WriteLine(json);
    }
}

public class Foo
{
    [JsonProperty(PropertyName = "KIBANA_INDEX")]
    public string KibanaIndex { get; set; } 
    
    public string AnotherProperty { get; set; }
}

输出:

{"KIBANA_INDEX":"whatever","anotherProperty":"whatever"}

fiddle :https://dotnetfiddle.net/0qeP3o

关于.net - 如何让 Newtonsoft.Json 以我想要的方式序列化属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67401935/

相关文章:

c# - 接口(interface)类型构造函数

c# - Newtonsoft JsonConverter 将字符串或对象转换为对象

c# - Entity Framework Core 5 : configuring many to many relationships, 如何指定外键名称

wpf - 如何在构建时使 .dll 和其他文件远离更深文件夹中的 .exe 文件?

Jquery DataTable 将参数传递给 ajax 调用 asp.net。无效的 JSON 原语

Azure 隔离计时器触发器功能在部署到 Azure 后不起作用

c# - linq2db:无法为表生成 POCO

.net - .net 4 和 EF 的 Sql 异常

c# - 添加 WCF 服务引用时,HTTP 请求被客户端身份验证方案 'Anonymous' 错误禁止

.net - 确保 .NET 中的 json 键是小写的