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

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

      Json.Net - 序列化不带引号的属性名称

      时间:2023-08-24

      <small id='2vvDG'></small><noframes id='2vvDG'>

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

          <tbody id='2vvDG'></tbody>
      1. <tfoot id='2vvDG'></tfoot>

          1. <legend id='2vvDG'><style id='2vvDG'><dir id='2vvDG'><q id='2vvDG'></q></dir></style></legend>
              <bdo id='2vvDG'></bdo><ul id='2vvDG'></ul>
                本文介绍了Json.Net - 序列化不带引号的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试让 Json.Net 序列化不带引号的属性名称,并且发现很难在 Google 上找到文档.我该怎么做?

                I'm trying to get Json.Net to serialise a property name without quote marks, and finding it difficult to locate documentation on Google. How can I do this?

                它在大型 Json 渲染的很小一部分中,所以我更喜欢添加一个属性属性,或者覆盖类上的序列化方法.

                It's in a very small part of a large Json render, so I'd prefer to either add a property attribute, or override the serialising method on the class.

                目前,它呈现如下:

                "event_modal":
                {
                    "href":"file.html",
                    "type":"full"
                }
                

                我希望让它呈现如下:(hreftype 没有引号)

                And I'm hoping to get it to render like: (href and type are without quotes)

                "event_modal":
                {
                    href:"file.html",
                    type:"full"
                }
                

                来自班级:

                public class ModalOptions
                {
                    public object href { get; set; }
                    public object type { get; set; }
                }
                

                推荐答案

                这是可能的,但 我不建议这样做,因为它会产生无效的 JSON,正如 Marcelo 和 Marc 在他们的评论中指出的那样.

                It's possible, but I advise against it as it would produce invalid JSON as Marcelo and Marc have pointed out in their comments.

                使用 Json.NET 库,您可以按如下方式实现:

                Using the Json.NET library you can achieve this as follows:

                [JsonObject(MemberSerialization.OptIn)]
                public class ModalOptions
                {
                    [JsonProperty]
                    public object href { get; set; }
                
                    [JsonProperty]
                    public object type { get; set; }
                }
                

                当序列化对象时,使用 JsonSerializer 类型而不是静态 JsonConvert 类型.

                When serializing the object use the JsonSerializer type instead of the static JsonConvert type.

                例如:

                var options = new ModalOptions { href = "file.html", type = "full" };
                var serializer = new JsonSerializer();
                var stringWriter = new StringWriter();
                using (var writer = new JsonTextWriter(stringWriter))
                {
                    writer.QuoteName = false;
                    serializer.Serialize(writer, options);            
                }
                var json = stringWriter.ToString();
                

                这将产生:

                {href:"file.html",type:"full"}
                

                如果您设置了 JsonTextWriter 实例的 QuoteName 属性为 false 将不再引用对象名称.

                If you set the QuoteName property of the JsonTextWriter instance to false the object names will no longer be quoted.

                这篇关于Json.Net - 序列化不带引号的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何调用 JsonConvert.DeserializeObject 并禁用通过 [J 下一篇:如何在反序列化期间以编程方式选择构造函数?

                相关文章

                最新文章

                • <bdo id='FUA7W'></bdo><ul id='FUA7W'></ul>
                <tfoot id='FUA7W'></tfoot>
                <legend id='FUA7W'><style id='FUA7W'><dir id='FUA7W'><q id='FUA7W'></q></dir></style></legend>

                  1. <small id='FUA7W'></small><noframes id='FUA7W'>

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