<i id='GMv2C'><tr id='GMv2C'><dt id='GMv2C'><q id='GMv2C'><span id='GMv2C'><b id='GMv2C'><form id='GMv2C'><ins id='GMv2C'></ins><ul id='GMv2C'></ul><sub id='GMv2C'></sub></form><legend id='GMv2C'></legend><bdo id='GMv2C'><pre id='GMv2C'><center id='GMv2C'></center></pre></bdo></b><th id='GMv2C'></th></span></q></dt></tr></i><div id='GMv2C'><tfoot id='GMv2C'></tfoot><dl id='GMv2C'><fieldset id='GMv2C'></fieldset></dl></div>

      • <bdo id='GMv2C'></bdo><ul id='GMv2C'></ul>

      <tfoot id='GMv2C'></tfoot>

    1. <legend id='GMv2C'><style id='GMv2C'><dir id='GMv2C'><q id='GMv2C'></q></dir></style></legend>

      <small id='GMv2C'></small><noframes id='GMv2C'>

    2. 确保 .NET 中的 json 键是小写的

      时间:2023-08-24
    3. <i id='6Yohg'><tr id='6Yohg'><dt id='6Yohg'><q id='6Yohg'><span id='6Yohg'><b id='6Yohg'><form id='6Yohg'><ins id='6Yohg'></ins><ul id='6Yohg'></ul><sub id='6Yohg'></sub></form><legend id='6Yohg'></legend><bdo id='6Yohg'><pre id='6Yohg'><center id='6Yohg'></center></pre></bdo></b><th id='6Yohg'></th></span></q></dt></tr></i><div id='6Yohg'><tfoot id='6Yohg'></tfoot><dl id='6Yohg'><fieldset id='6Yohg'></fieldset></dl></div>

          <bdo id='6Yohg'></bdo><ul id='6Yohg'></ul>

            <small id='6Yohg'></small><noframes id='6Yohg'>

            <legend id='6Yohg'><style id='6Yohg'><dir id='6Yohg'><q id='6Yohg'></q></dir></style></legend>
                  <tbody id='6Yohg'></tbody>
                <tfoot id='6Yohg'></tfoot>
                本文介绍了确保 .NET 中的 json 键是小写的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                Is there simple way using JSON in .NET to ensure that the keys are sent as lower case?

                At the moment I'm using the newtonsoft's Json.NET library and simply using

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

                This results in

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

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

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

                I realise I can programatically do this and create the JSON string manually, but I need this for approx 20 or so JSON data strings and I'm seeing if I can save myself some time. I'm wondering if there are any already built 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();
                    }
                }
                

                Usage:

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

                Wil result in:

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


                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

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

                这篇关于确保 .NET 中的 json 键是小写的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:有选择地使用默认 JSON 转换器 下一篇:使用 Json.Net 解析 JSON 数组

                相关文章

                最新文章

                    <small id='kzuC3'></small><noframes id='kzuC3'>

                  1. <tfoot id='kzuC3'></tfoot>
                    <i id='kzuC3'><tr id='kzuC3'><dt id='kzuC3'><q id='kzuC3'><span id='kzuC3'><b id='kzuC3'><form id='kzuC3'><ins id='kzuC3'></ins><ul id='kzuC3'></ul><sub id='kzuC3'></sub></form><legend id='kzuC3'></legend><bdo id='kzuC3'><pre id='kzuC3'><center id='kzuC3'></center></pre></bdo></b><th id='kzuC3'></th></span></q></dt></tr></i><div id='kzuC3'><tfoot id='kzuC3'></tfoot><dl id='kzuC3'><fieldset id='kzuC3'></fieldset></dl></div>
                    <legend id='kzuC3'><style id='kzuC3'><dir id='kzuC3'><q id='kzuC3'></q></dir></style></legend>

                    • <bdo id='kzuC3'></bdo><ul id='kzuC3'></ul>