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

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

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

        使用 Json.NET 使用新的部分 JSON 数据修改现有对象

        时间:2023-08-23

        <small id='2rgDf'></small><noframes id='2rgDf'>

          <tbody id='2rgDf'></tbody>

          • <legend id='2rgDf'><style id='2rgDf'><dir id='2rgDf'><q id='2rgDf'></q></dir></style></legend>
              <tfoot id='2rgDf'></tfoot>
                <bdo id='2rgDf'></bdo><ul id='2rgDf'></ul>

                  <i id='2rgDf'><tr id='2rgDf'><dt id='2rgDf'><q id='2rgDf'><span id='2rgDf'><b id='2rgDf'><form id='2rgDf'><ins id='2rgDf'></ins><ul id='2rgDf'></ul><sub id='2rgDf'></sub></form><legend id='2rgDf'></legend><bdo id='2rgDf'><pre id='2rgDf'><center id='2rgDf'></center></pre></bdo></b><th id='2rgDf'></th></span></q></dt></tr></i><div id='2rgDf'><tfoot id='2rgDf'></tfoot><dl id='2rgDf'><fieldset id='2rgDf'></fieldset></dl></div>
                1. 本文介绍了使用 Json.NET 使用新的部分 JSON 数据修改现有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  考虑下面的示例程序

                  var calendar = new Calendar
                  {
                      Id = 42,
                      CoffeeProvider = "Espresso2000",
                      Meetings = new[]
                      {
                          new Meeting
                          {
                              Location = "Room1",
                              From = DateTimeOffset.Parse("2014-01-01T00:00:00Z"),
                              To = DateTimeOffset.Parse("2014-01-01T01:00:00Z")
                          },
                          new Meeting
                          {
                              Location = "Room2",
                              From = DateTimeOffset.Parse("2014-01-01T02:00:00Z"),
                              To = DateTimeOffset.Parse("2014-01-01T03:00:00Z")
                          },
                      }
                  };
                  
                  var patch = @"{
                          'coffeeprovider': null,
                          'meetings': [
                              {
                                  'location': 'Room3',
                                  'from': '2014-01-01T04:00:00Z',
                                  'to': '2014-01-01T05:00:00Z'
                              }
                          ]
                      }";
                  
                  var patchedCalendar = Patch(calendar, patch);
                  

                  Patch() 方法的结果应等于calendar,除非由patch 更改.这意味着;Id 将保持不变,CoffeeProvider 将设置为 nullMeetings 将包含位于 Room3 中的单个项目.

                  The result of the Patch() method should be equal to calendar except as changed by patch. That means; Id would be unchanged, CoffeeProvider would be set to null and Meetings would contain a single item located in Room3.

                  1. 如何创建一个通用的 Patch() 方法可反序列化的任何对象(不仅仅是示例 Calendar 对象)Json.NET?

                  1. How does one create a general Patch() method that will work for any object (not just the example Calendar object) deserializable by Json.NET?

                  如果 (1) 这不可行,会有哪些限制使其可行以及如何实施?

                  If (1) this is not feasible, what would be some restrictions that would make it feasible and how would it be implemented?

                  推荐答案

                  你想要 JsonSerializer.Populate() 或其静态包装方法 JsonConvert.PopulateObject():

                  将 JSON 值填充到目标对象上.

                  Populates the JSON values onto the target object.

                  例如,这里它正在更新您的 Calendar 类的实例:

                  For instance, here it is updating an instance of your Calendar class:

                  public static class TestPopulate
                  {
                      public static void Test()
                      {
                          var calendar = new Calendar
                          {
                              Id = 42,
                              CoffeeProvider = "Espresso2000",
                              Meetings = new[]
                              {
                                  new Meeting
                                  {
                                      Location = "Room1",
                                      From = DateTimeOffset.Parse("2014-01-01T00:00:00Z"),
                                      To = DateTimeOffset.Parse("2014-01-01T01:00:00Z")
                                  },
                                  new Meeting
                                  {
                                      Location = "Room2",
                                      From = DateTimeOffset.Parse("2014-01-01T02:00:00Z"),
                                      To = DateTimeOffset.Parse("2014-01-01T03:00:00Z")
                                  },
                              }
                          };
                  
                          var patch = @"{
                      'coffeeprovider': null,
                      'meetings': [
                          {
                              'location': 'Room3',
                              'from': '2014-01-01T04:00:00Z',
                              'to': '2014-01-01T05:00:00Z'
                          }
                      ]
                  }";
                          Patch(calendar, patch);
                  
                          Debug.WriteLine(JsonConvert.SerializeObject(calendar, Formatting.Indented));
                      }
                  
                      public static void Patch<T>(T obj, string patch)
                      {
                          var serializer = new JsonSerializer();
                          using (var reader = new StringReader(patch))
                          {
                              serializer.Populate(reader, obj);
                          }
                      }
                  }
                  

                  产生的调试输出是:

                  {
                    "id": 42,
                    "coffeeprovider": null,
                    "meetings": [
                      {
                        "location": "Room3",
                        "from": "2014-01-01T04:00:00+00:00",
                        "to": "2014-01-01T05:00:00+00:00"
                      }
                    ]
                  }
                  

                  更新

                  如果你想先复制,你可以这样做:

                  If you want to copy first, you could do:

                      public static T CopyPatch<T>(T obj, string patch)
                      {
                          var serializer = new JsonSerializer();
                  
                          var json = JsonConvert.SerializeObject(obj);
                          var copy = JsonConvert.DeserializeObject<T>(json);
                  
                          using (var reader = new StringReader(patch))
                          {
                              serializer.Populate(reader, copy);
                          }
                  
                          return copy;
                      }
                  

                  这篇关于使用 Json.NET 使用新的部分 JSON 数据修改现有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C# 类如何处理 JSON 中的美元符号? 下一篇:覆盖 Json.Net 中的默认原始类型处理

                  相关文章

                  最新文章

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

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

                  2. <legend id='jMdS8'><style id='jMdS8'><dir id='jMdS8'><q id='jMdS8'></q></dir></style></legend>
                  3. <small id='jMdS8'></small><noframes id='jMdS8'>