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

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

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

  • <tfoot id='lLAFU'></tfoot>
    1. <legend id='lLAFU'><style id='lLAFU'><dir id='lLAFU'><q id='lLAFU'></q></dir></style></legend>

        对属性的 Json DeserializeObject 运行转换

        时间:2023-05-22
        1. <tfoot id='lSqGo'></tfoot>
                <i id='lSqGo'><tr id='lSqGo'><dt id='lSqGo'><q id='lSqGo'><span id='lSqGo'><b id='lSqGo'><form id='lSqGo'><ins id='lSqGo'></ins><ul id='lSqGo'></ul><sub id='lSqGo'></sub></form><legend id='lSqGo'></legend><bdo id='lSqGo'><pre id='lSqGo'><center id='lSqGo'></center></pre></bdo></b><th id='lSqGo'></th></span></q></dt></tr></i><div id='lSqGo'><tfoot id='lSqGo'></tfoot><dl id='lSqGo'><fieldset id='lSqGo'></fieldset></dl></div>

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

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

                    <tbody id='lSqGo'></tbody>
                • <legend id='lSqGo'><style id='lSqGo'><dir id='lSqGo'><q id='lSqGo'></q></dir></style></legend>
                  本文介绍了对属性的 Json DeserializeObject 运行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  假设一个json字符串如下:

                  Assuming a json string like the following:

                  string json = '{"string_property":"foo_bar", ... other objects here ...}';
                  

                  我想知道是否有一种方法可以对解析的对象运行转换,以便在运行以下方法后得到 foo_bar 而不是获取 foo_bar(可以是任何东西)

                  I was wondering if there's a way to run a transformation on the parsed object so that instead of getting foo_bar, I'll get foo bar after running the following method (can be anything really)

                  public string Transform(string s) {
                      return s.Replace("_"," ");
                  }
                  

                  我可以在反序列化后手动更改我的 poco,但想知道什么是更清洁"的方法?

                  I can manually alter my poco after deserializing, but wondered what would be a "cleaner" approach?

                  推荐答案

                  您可以在反序列化根对象时转换您的 string 属性,方法是使用 自定义 JsonConverter 针对所有字符串类型值:

                  You can transform your string properties as you deserialize your root object by using a custom JsonConverter targeted at all string type values:

                  public class ReplacingStringConverter : JsonConverter
                  {
                      readonly string oldValue;
                      readonly string newValue;
                  
                      public ReplacingStringConverter(string oldValue, string newValue)
                      {
                          if (string.IsNullOrEmpty(oldValue))
                              throw new ArgumentException("string.IsNullOrEmpty(oldValue)");
                          if (newValue == null)
                              throw new ArgumentNullException("newValue");
                          this.oldValue = oldValue;
                          this.newValue = newValue;
                      }
                  
                      public override bool CanConvert(Type objectType)
                      {
                          return objectType == typeof(string);
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                      {
                          if (reader.TokenType == JsonToken.Null)
                              return null;
                          var s = (string)JToken.Load(reader);
                          return s.Replace(oldValue, newValue);
                      }
                  
                      public override bool CanWrite { get { return false; } }
                  
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                      {
                          throw new NotImplementedException();
                      }
                  }
                  

                  然后像这样使用它:

                  var settings = new JsonSerializerSettings { Converters = new[] { new ReplacingStringConverter("_", "") } };
                  var result = JsonConvert.DeserializeObject<RootObject>(json, settings);
                  

                  但是请注意,如果单个字符串类型属性有自己的转换器,则直接使用 [JsonConverter(Type)],这些转换器将优先于 ReplacingStringConverter 使用.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_Converters.htm" rel="nofollow noreferrer">转换器列表.

                  Note however that if individual string-type properties have their own converters applied directly with [JsonConverter(Type)], those converters will be used in preference to the ReplacingStringConverter in the Converters list.

                  这篇关于对属性的 Json DeserializeObject 运行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将空引用分析的结果写入日志文件 下一篇:Json.net 无法加载属于类对象的某些属性?

                  相关文章

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

                      • <bdo id='wOLcY'></bdo><ul id='wOLcY'></ul>
                    1. <tfoot id='wOLcY'></tfoot>

                    2. <legend id='wOLcY'><style id='wOLcY'><dir id='wOLcY'><q id='wOLcY'></q></dir></style></legend>

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