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

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

  2. <small id='F0gUu'></small><noframes id='F0gUu'>

      <bdo id='F0gUu'></bdo><ul id='F0gUu'></ul>

    1. 无法使用 Json.NET 将枚举正确转换为 json

      时间:2023-08-23

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

          <tbody id='9W2dH'></tbody>
      1. <legend id='9W2dH'><style id='9W2dH'><dir id='9W2dH'><q id='9W2dH'></q></dir></style></legend>

              <bdo id='9W2dH'></bdo><ul id='9W2dH'></ul>
              <tfoot id='9W2dH'></tfoot>
                本文介绍了无法使用 Json.NET 将枚举正确转换为 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个枚举:

                public enum Animal 
                { 
                    Dog, 
                    Cat, 
                    BlackBear 
                }
                

                我需要将其发送到第三方 API.此 API 要求我发送的枚举值必须小写,并且偶尔需要下划线.一般来说,他们需要的名称与我使用的枚举命名约定不匹配.

                I need to send it to a third-party API. This API requires that the enum values I send be lower case and occasionally require underscores. In general, the names they require don't match the enum naming convention I use.

                使用 https 中提供的示例://gooddevbaddev.wordpress.com/2013/08/26/deserializing-c-enums-using-json-net/,我尝试使用自定义的JsonConverter:

                Using the example provided at https://gooddevbaddev.wordpress.com/2013/08/26/deserializing-c-enums-using-json-net/, I tried to use a custom JsonConverter:

                public class AnimalConverter : JsonConverter {
                    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
                        var animal = (Animal)value;
                        switch (animal)
                        {
                            case Animal.Dog:
                            {
                                writer.WriteValue("dog");
                                break;
                            }
                            case Animal.Cat:
                            {
                                writer.WriteValue("cat");
                                break;
                            }
                            case Animal.BlackBear:
                            {
                                writer.WriteValue("black_bear");
                                break;
                            }
                        }
                    }
                    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
                        var enumString = (string)reader.Value;
                        Animal? animal = null;
                        switch (enumString)
                        {
                            case "cat":
                            {
                                animal = Animal.Cat;
                                break;
                            }
                            case "dog":
                            {
                                animal = Animal.Dog;
                                break;
                            }
                            case "black_bear":
                            {
                                animal = Animal.BlackBear;
                                break;
                            }
                        }
                    }
                
                    public override bool CanConvert(Type objectType)
                    {
                        return objectType == typeof(string);
                    }
                }
                

                回到类的属性,我把属性放在 Animal 上:

                Back in the properties of a class, I put the attributes on the Animal as so:

                [JsonProperty("animal")]
                [JsonConverter(typeof(AnimalConverter))]
                public Animal ZooAnimals { get; set; }
                

                但是,当我运行程序时,它似乎完全忽略了 JsonConverter,而不是看到像black_bear"或dog"这样的预期值,而是看到了BlackBear"和Dog".如何让 JsonConverter 实际执行从枚举值的名称到我指定替换该值的字符串的转换?

                When I run the program though, it seems to completely ignore the JsonConverter and rather than seeing expected values like "black_bear" or "dog", I see "BlackBear" and "Dog". How can I get the JsonConverter to actually do the conversion from the name of the enum value to the string I specify to replace that value with?

                谢谢!

                推荐答案

                您不需要编写自己的转换器.Json.NET 的 StringEnumConverter 将读取 <一个 href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.enummemberattribute.aspx" rel="noreferrer">EnumMember 属性.如果您将 enum 更改为此,它将序列化到您想要的值.

                You don't need to write your own converter. Json.NET's StringEnumConverter will read the EnumMember attribute. If you change your enum to this, it will serialize from and to the values you want.

                [JsonConverter(typeof(StringEnumConverter))]
                public enum Animals 
                {
                    [EnumMember(Value = "dog")]
                    Dog, 
                    [EnumMember(Value = "cat")]
                    Cat, 
                    [EnumMember(Value = "black_bear")]
                    BlackBear 
                }
                

                (作为一个小提示,由于 Animals 不是标志枚举,它 应该是单数:Animal.你应该考虑改成这个.)

                (As a minor note, since Animals isn't a flags enum, it should be singular: Animal. You should consider changing it to this.)

                这篇关于无法使用 Json.NET 将枚举正确转换为 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 NewtonSoft JsonConvert 反序列化名称中带有 下一篇:使用 Json.net 编写 JSON 时如何打开缩进?

                相关文章

                最新文章

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

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

                    <bdo id='mapBp'></bdo><ul id='mapBp'></ul>

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