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

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

        Json.net 重命名属性

        时间:2023-08-23

            <tfoot id='0bBkX'></tfoot>
              <bdo id='0bBkX'></bdo><ul id='0bBkX'></ul>

              <small id='0bBkX'></small><noframes id='0bBkX'>

                <tbody id='0bBkX'></tbody>

                  <legend id='0bBkX'><style id='0bBkX'><dir id='0bBkX'><q id='0bBkX'></q></dir></style></legend>
                  <i id='0bBkX'><tr id='0bBkX'><dt id='0bBkX'><q id='0bBkX'><span id='0bBkX'><b id='0bBkX'><form id='0bBkX'><ins id='0bBkX'></ins><ul id='0bBkX'></ul><sub id='0bBkX'></sub></form><legend id='0bBkX'></legend><bdo id='0bBkX'><pre id='0bBkX'><center id='0bBkX'></center></pre></bdo></b><th id='0bBkX'></th></span></q></dt></tr></i><div id='0bBkX'><tfoot id='0bBkX'></tfoot><dl id='0bBkX'><fieldset id='0bBkX'></fieldset></dl></div>
                  本文介绍了Json.net 重命名属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个表示 JSON 的字符串,我想使用 JSON.NET 重命名一些属性.我需要一个通用函数来用于任何 JSON.比如:

                  I have a string representing JSON and I want to rename some of the properties using JSON.NET. I need a generic function to use for any JSON. Something like:

                  public static void Rename(JContainer container, Dictiontionary<string, string> mapping)
                  {
                    foreach (JToken el in container.Children())
                    {
                      JProperty p = el as JProperty;
                      if(el != null && mapping.ContainsKey(p.Name))
                      {
                        // **RENAME THIS NODE!!**
                      }
                  
                      // recursively rename nodes
                      JContainer pcont = el as JContainer;
                      if(pcont != null)
                      {
                        Rename(pcont, mapping);
                      }
                    }
                  }
                  

                  怎么做??

                  推荐答案

                  我建议用重命名的属性重构你的 JSON.我认为您不应该担心速度处罚,因为这通常不是问题.以下是你可以做到的.

                  I would suggest reconstructing your JSON with renamed properties. I don't think you should worry about speed penalties as it's usually not an issue. Here's how you can do it.

                  public static JToken Rename(JToken json, Dictionary<string, string> map)
                  {
                      return Rename(json, name => map.ContainsKey(name) ? map[name] : name);
                  }
                  
                  public static JToken Rename(JToken json, Func<string, string> map)
                  {
                      JProperty prop = json as JProperty;
                      if (prop != null) 
                      {
                          return new JProperty(map(prop.Name), Rename(prop.Value, map));
                      }
                  
                      JArray arr = json as JArray;
                      if (arr != null)
                      {
                          var cont = arr.Select(el => Rename(el, map));
                          return new JArray(cont);
                      }
                  
                      JObject o = json as JObject;
                      if (o != null)
                      {
                          var cont = o.Properties().Select(el => Rename(el, map));
                          return new JObject(cont);
                      }
                  
                      return json;
                  }
                  

                  下面是一个用法示例:

                  var s = @"{ ""A"": { ""B"": 1, ""Test"": ""123"", ""C"": { ""Test"": [ ""1"", ""2"", ""3"" ] } } }";
                  var json = JObject.Parse(s);
                  
                  var renamed = Rename(json, name => name == "Test" ? "TestRenamed" : name);
                  renamed.ToString().Dump();  // LINQPad output
                  
                  var dict = new Dictionary<string, string> { { "Test", "TestRenamed"} };
                  var renamedDict = Rename(json, dict);
                  renamedDict.ToString().Dump();  // LINQPad output
                  

                  这篇关于Json.net 重命名属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:自定义反序列化器仅适用于带有 json.NET 的某些字 下一篇:Json.Net PopulateObject - 根据 ID 更新列表元素

                  相关文章

                  最新文章

                    <tfoot id='4lwfw'></tfoot>

                    <small id='4lwfw'></small><noframes id='4lwfw'>

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