.NET确保JSON键是小写NET、JSON

2023-09-02 01:44:29 作者:习惯被爱Boy

大家好有没有使用JSON在.net中,以确保密钥将作为小写简单的方式。

Hi all is there simple way using json in .Net to ensure that the keys are sent as lower case.

目前我使用的newtonsoft.json库,并简单地用

At the moment I'm using the newtonsoft.json library and simply using

字符串loginRequest = JsonConvert.SerializeObject(AUTH);

在这种情况下,身份验证只是下面的对象

In this case auth is just the following object

public class Authority
{
    public string Username { get; set; }
    public string ApiToken { get; set; }
}

这导致了

{"Username":"Mark","ApiToken":"xyzABC1234"}

有没有去确保用户名和apitoken键来通过为小写?

Is there away to ensure that the username and apitoken keys come through as lowercase ?

我不希望只是通过课程string.tolower()运行它,因为用户名和安培值; apitoken混合情况。

I don't want to simply run it through string.tolower() of course because the values for username & apitoken are mixed case.

我知道我可以programically做到这一点,手动创建JSON字符串,但我需要这个大约20个左右的JSON数据串,看是否我能拯救自己一些时间。不知道是否有任何已经构建库,以便执行小写字母用于密钥创建的。

I realise I can programically do this and create the json string manually, but i need this for approx 20 or so json data strings and seeing if i can save myself some time. Wondering if there are any already build libraries that allow you to enforce lowercase for key creation.

推荐答案

您可以创建自定义的合同解析这一点。以下合同解析器将所有键转换为小写:

You can create a custom contract resolver for this. The following contract resolver will convert all keys to lowercase:

public class LowercaseContractResolver : DefaultContractResolver
{
    protected override string ResolvePropertyName(string propertyName)
    {
        return propertyName.ToLower();
    }
}

用法:

var settings = new JsonSerializerSettings();
settings.ContractResolver = new LowercaseContractResolver();
var json = JsonConvert.SerializeObject(authority, Formatting.Indented, settings);

威尔结果:

{"username":"Mark","apitoken":"xyzABC1234"}

如果你总是希望使用 LowercaseContractResolver ,可以考虑将其包装在一个类中,以避免重蹈自己的序列化:

If you always want to serialize using the LowercaseContractResolver, consider wrapping it in a class to avoid repeating yourself:

public class LowercaseJsonSerializer
{
    private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        ContractResolver = new LowercaseContractResolver()
    };

    public static string SerializeObject(object o)
    {
        return JsonConvert.SerializeObject(o, Formatting.Indented, Settings);
    }

    public class LowercaseContractResolver : DefaultContractResolver
    {
        protected override string ResolvePropertyName(string propertyName)
        {
            return propertyName.ToLower();
        }
    }
}

这可以是这样的:

Which can be used like this:

var json = LowercaseJsonSerializer.SerializeObject(new { Foo = "bar" });
// { "foo": "bar" }

ASP.NET MVC4 /的WebAPI

JsonNet

如果您使用的是ASP.NET MVC4 /的WebAPI,您可以使用 CamelCasePropertyNamesContractResolver 从Newtonsoft.Json库,其中包括默认情况下。

ASP.NET MVC4 / WebAPI

If you are using ASP.NET MVC4 / WebAPI, you can use a CamelCasePropertyNamesContractResolver from Newtonsoft.Json library which included by default.