久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" id='9W2dH'><tfoot id='9W2dH'></tfoot><dl id='9W2dH'><fieldset id='9W2dH'></fieldset></dl></div>
  • <small id='9W2dH'></small><noframes id='9W2dH'>

    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

        Can#39;t get enum to convert to json properly using Json.NET(無法使用 Json.NET 將枚舉正確轉換為 json)

            <tbody id='F0gUu'></tbody>
          <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 class="qwawimqqmiuu" id='F0gUu'><tfoot id='F0gUu'></tfoot><dl id='F0gUu'><fieldset id='F0gUu'></fieldset></dl></div>

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

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

                • 本文介紹了無法使用 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 將讀取 <一個 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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯誤的 XML)

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

                    • <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 class="qwawimqqmiuu" id='mapBp'><tfoot id='mapBp'></tfoot><dl id='mapBp'><fieldset id='mapBp'></fieldset></dl></div>
                    • <tfoot id='mapBp'></tfoot>

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

                            <tbody id='mapBp'></tbody>
                          <legend id='mapBp'><style id='mapBp'><dir id='mapBp'><q id='mapBp'></q></dir></style></legend>
                            主站蜘蛛池模板: h片免费在线观看 | 国产精品美女久久久久久免费 | 久久精品国产亚洲 | 中文字幕一区二区三区四区五区 | 在线免费视频一区 | 亚洲视频不卡 | 色偷偷人人澡人人爽人人模 | 99免费在线视频 | 日韩美女爱爱 | 国产日产精品一区二区三区四区 | www.com久久久 | 成人蜜桃av | 一级黄片一级毛片 | 男女搞网站 | 国产成人精品一区二区三区四区 | 午夜免费 | 国产精品www | 狠狠干天天干 | 国产电影一区二区 | 久久久久久久久久一区二区 | 国产成人影院 | 午夜精品视频在线观看 | 热久久国产 | 国产精品日本一区二区在线播放 | 91社区在线高清 | 欧美一区二区免费 | 欧美一级艳情片免费观看 | 精品美女视频在线观看免费软件 | 国产精品福利久久久 | 成人h视频 | 国产区一区二区三区 | 久久久久91 | 亚洲成人综合在线 | www.蜜桃av.com| 热99| 午夜视频在线播放 | 91亚洲国产成人久久精品网站 | 日韩精品一区二区三区 | 欧美日本亚洲 | 三级成人片 | 国产电影一区 |