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

        <bdo id='LVPZo'></bdo><ul id='LVPZo'></ul>
      <tfoot id='LVPZo'></tfoot>

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

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

      Newtonsoft JsonSerializer - 小写属性和字典

      时间:2023-08-22

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

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

              • <small id='RtOWy'></small><noframes id='RtOWy'>

                本文介绍了Newtonsoft JsonSerializer - 小写属性和字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在使用 json.net(Newtonsoft 的 JsonSerializer).我需要自定义序列化以满足以下要求:

                I'm using json.net (Newtonsoft's JsonSerializer). I need to customize serialization in order to meet following requirements:

                1. 属性名称必须以小写字母开头.
                2. 字典必须序列化为 jsonp,其中的键将用于属性名称.小写规则不适用于字典键.

                例如:

                var product = new Product();
                procuct.Name = "Product1";
                product.Items = new Dictionary<string, Item>();
                product.Items.Add("Item1", new Item { Description="Lorem Ipsum" });
                

                必须序列化为:

                {
                  name: "Product1",
                  items : {
                    "Item1": {
                       description : "Lorem Ipsum"
                    }
                  }
                }
                

                注意属性 Name 序列化为name",但键 Item1 序列化为Item1";

                notice that property Name serializes into "name", but key Item1 serializes into "Item1";

                我尝试创建 CustomJsonWriter 来序列化属性名称,但它也会更改字典键.

                I have tried to create CustomJsonWriter to serialize property names, but it changes also dicionary keys.

                public class CustomJsonWriter : JsonTextWriter
                {
                    public CustomJsonWriter(TextWriter writer) : base(writer)
                    {
                
                    }
                    public override void WritePropertyName(string name, bool escape)
                    {
                        if (name != "$type")
                        {
                            name = name.ToCamelCase();
                        }
                        base.WritePropertyName(name, escape);
                    }
                }
                

                推荐答案

                您可以尝试使用 CamelCasePropertyNamesContractResolver.

                var serializerSettings = new JsonSerializerSettings();
                serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                var json = JsonConvert.SerializeObject(product, serializerSettings);
                

                我只是不确定它将如何处理字典键,而且我现在没有时间尝试它.如果它不能正确处理密钥,那么未来仍然值得牢记,而不是编写自己的自定义 JSON 编写器.

                I'm just not sure how it'll handle the dictionary keys and I don't have time right this second to try it. If it doesn't handle the keys correctly it's still worth keeping in mind for the future rather than writing your own custom JSON writer.

                这篇关于Newtonsoft JsonSerializer - 小写属性和字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:从 Json 字符串中提取数据 下一篇:访问 JToken 中的所有项目

                相关文章

                最新文章

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

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

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

                  1. <tfoot id='XIrhw'></tfoot>