• <tfoot id='nYFBK'></tfoot>

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

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

        如何在 JObject 中添加或更新 JProperty 值

        时间:2023-08-22
        <legend id='S4Voa'><style id='S4Voa'><dir id='S4Voa'><q id='S4Voa'></q></dir></style></legend>
        1. <tfoot id='S4Voa'></tfoot>

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

                <tbody id='S4Voa'></tbody>
                <i id='S4Voa'><tr id='S4Voa'><dt id='S4Voa'><q id='S4Voa'><span id='S4Voa'><b id='S4Voa'><form id='S4Voa'><ins id='S4Voa'></ins><ul id='S4Voa'></ul><sub id='S4Voa'></sub></form><legend id='S4Voa'></legend><bdo id='S4Voa'><pre id='S4Voa'><center id='S4Voa'></center></pre></bdo></b><th id='S4Voa'></th></span></q></dt></tr></i><div id='S4Voa'><tfoot id='S4Voa'></tfoot><dl id='S4Voa'><fieldset id='S4Voa'></fieldset></dl></div>
                • <bdo id='S4Voa'></bdo><ul id='S4Voa'></ul>
                  本文介绍了如何在 JObject 中添加或更新 JProperty 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我目前正在使用以下扩展方法来执行此任务,但似乎应该有一些现有的包含方法或扩展来执行此任务(或至少是其中的一个子集).如果 Json.NET 中没有任何内容,那么推荐的流程是什么,或者我将如何更改下面的代码以更接近推荐的流程.

                  I am currently using the following extension method to perform this task, but it almost seems like there should be some existing included method or extension to perform this (or at least a subset of this). If there isn't anything within Json.NET then what is the recommended process, or how would I change the code below to be closer to the recommended process.

                  public static partial class ExtensionMethods
                  {
                      public static JObject SetPropertyContent(this JObject source, string name, object content)
                      {
                          var prop = source.Property(name);
                  
                          if (prop == null)
                          {
                              prop = new JProperty(name, content);
                  
                              source.Add(prop);
                          }
                          else
                          {
                              prop.Value = JContainer.FromObject(content);
                          }
                  
                          return source;
                      }
                  }
                  

                  我可以确认上面的代码适用于基本用途,但我不确定它是否适用于更广泛的用途.

                  I can confirm the above code works for basic usage, but I'm not certain how well it holds up to broader usage.

                  我让这个扩展返回一个 JObject 的原因是你可以链接调用(对这个扩展或其他方法和扩展的多次调用).

                  The reason I have this extension returning a JObject is so that you would be able to chain calls (either multiple calls to this extension or to other methods and extensions).

                  即,

                  var data = JObject.Parse("{ 'str1': 'test1' }");
                  
                  data
                      .SetPropertyContent("str1", "test2")
                      .SetPropertyContent("str3", "test3");
                  
                  // {
                  //   "str1": "test2",
                  //   "str3": "test3"
                  // }
                  

                  推荐答案

                  正如@dbc 在评论中描述的那样,您可以简单地使用索引器来实现这一点.

                  as @dbc described in the comment, you can simply use the indexer to make this happen.

                  var item = JObject.Parse("{ 'str1': 'test1' }");
                  
                  item["str1"] = "test2";
                  item["str3"] = "test3";
                  

                  查看 fiddle 了解更多详情

                  这篇关于如何在 JObject 中添加或更新 JProperty 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何返回 JSON 对象 下一篇:通过连接包含的元素来合并两个 Json.NET 数组

                  相关文章

                  最新文章

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

                  <tfoot id='xo4SL'></tfoot>

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

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