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

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

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

        将两个 JToken 合并为一个

        时间:2023-08-22
        <legend id='AjawX'><style id='AjawX'><dir id='AjawX'><q id='AjawX'></q></dir></style></legend>
        • <tfoot id='AjawX'></tfoot>

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

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

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

                  <tbody id='AjawX'></tbody>
                • 本文介绍了将两个 JToken 合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何将这两个 JToken 合并为一个 JToken.这听起来应该很简单,但我无法绕过它.

                  <代码>{数据":[{"ID":"53a1862000404a304942546b35519ba3","name":"私人审批流程:CPL 文件草案","objCode":"ARVPTH"}]}{数据":[{"ID":"53a1838200401324eb1ec66562e9d77d","name":"私人审批流程:CPL 文件草案","objCode":"ARVPTH"}]}

                  感谢您的帮助!

                  这是我迄今为止尝试过的:

                  我首先将第一个对象分配给变量Jtoken pageOne,然后我尝试将它连接到第二个变量JToken allPages.我有一个循环,可以带回多个页面,每个页面包含三个字段.最终目标是抓取每个页面并创建一个包含所有页面的大 JToken.

                  类似这样的:

                  for (int page = 0; page <= recCount; page += 2000){//获取数据pageOne = getJsonData();allPages.Concat(pageOne);}返回所有页面;

                  解决方案

                  你可以使用 JContainer.Merge(Object, JsonMergeSettings) 将一个 JObject 合并到另一个.请注意,JsonMergeSettings.MergeArrayHandling 提供控制权关于如何合并数组.在 MergeArrayHandling 枚举文档中,可能的合并选项是:

                  <块引用>

                  Concat 0 连接数组.Union 1 联合数组,跳过已经存在的项目.替换 2 替换所有数组项.合并 3 将数组项合并在一起,按索引匹配.

                  因此使用 MergeArrayHandling.Concat 合并如下,其中 allPagespageOne 都是 JContainer 类型(或子类,例如 JObject):

                  JContainer allPages = null;var settings = new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat };for (int page = 0; page <= recCount; page += 2000){//获取数据var pageOne = (JContainer)getJsonData(page);如果(所有页面 == 空)allPages = pageOne;别的allPages.Merge(pageOne, 设置);}返回所有页面;

                  给予:

                  <代码>{数据":[{ID":53a1862000404a304942546b35519ba3",名称":私人审批流程:CPL 文件草案",objCode":ARVPTH"},{ID":53a1838200401324eb1ec66562e9d77d",名称":私人审批流程:CPL 文件草案",objCode":ARVPTH"}]}

                  使用 Replace 进行合并时:

                  <代码>{数据":[{ID":53a1838200401324eb1ec66562e9d77d",名称":私人审批流程:CPL 文件草案",objCode":ARVPTH"}]}

                  如果您的变量是 JToken 类型,则需要将它们强制转换为 JContainer.(不能合并非容器的 JSON 基元.)

                  JsonMergeSettings.MergeNullValueHandling 给出根据需要控制是否合并或忽略 null 值.

                  How can I merge these two JTokens into one single JToken. This sounds like it should be simple, but can't get my way around it.

                  {
                    "data":[
                    {
                        "ID":"53a1862000404a304942546b35519ba3",
                        "name":"Private Approval Process: Draft Document CPL",
                        "objCode":"ARVPTH"
                    }]
                  }
                  
                  {
                  "data":[
                    {
                        "ID":"53a1838200401324eb1ec66562e9d77d",
                        "name":"Private Approval Process: Draft Document CPL",
                        "objCode":"ARVPTH"
                    }]
                  }
                  

                  Thanks for the help!

                  This is what I have tried so far:

                  I started by assigning the first object to a variable Jtoken pageOne then, I tried concatenating it into a second variable JToken allPages. I have a loop that brings back multiple pages each with three fields. The final goal is to grab each page and create a big JToken with all of the pages in it.

                  something like this:

                  for (int page = 0; page <= recCount; page += 2000)
                  {
                   //Get data
                   pageOne = getJsonData();
                   allPages.Concat(pageOne);
                  }
                  return allPages;
                  

                  解决方案

                  You can use JContainer.Merge(Object, JsonMergeSettings) to merge one JObject onto another. Note that JsonMergeSettings.MergeArrayHandling gives control over how arrays are merged. From the MergeArrayHandling Enumeration documentation, the possible merge options are:

                  Concat   0   Concatenate arrays.
                  Union    1   Union arrays, skipping items that already exist.
                  Replace  2   Replace all array items.
                  Merge    3   Merge array items together, matched by index. 
                  

                  Thus merging using MergeArrayHandling.Concat as follows, where allPages and pageOne are both of type JContainer (or a subclass, such as JObject):

                  JContainer allPages = null;
                  var settings = new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat };
                  for (int page = 0; page <= recCount; page += 2000)
                  {
                      //Get data
                      var pageOne = (JContainer)getJsonData(page);
                      if (allPages == null)
                          allPages = pageOne;
                      else
                          allPages.Merge(pageOne, settings);
                  }
                  return allPages;
                  

                  gives:

                  {
                    "data": [
                      {
                        "ID": "53a1862000404a304942546b35519ba3",
                        "name": "Private Approval Process: Draft Document CPL",
                        "objCode": "ARVPTH"
                      },
                      {
                        "ID": "53a1838200401324eb1ec66562e9d77d",
                        "name": "Private Approval Process: Draft Document CPL",
                        "objCode": "ARVPTH"
                      }
                    ]
                  }
                  

                  While merging using Replace gives:

                  {
                    "data": [
                      {
                        "ID": "53a1838200401324eb1ec66562e9d77d",
                        "name": "Private Approval Process: Draft Document CPL",
                        "objCode": "ARVPTH"
                      }
                    ]
                  }
                  

                  If your variables are of type JToken you will need to cast them to JContainer. (JSON primitives that are not containers cannot be merged.)

                  JsonMergeSettings.MergeNullValueHandling gives control over whether to merge or ignore null values, as required.

                  这篇关于将两个 JToken 合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:json 网络前导零(禁用基本转换) 下一篇:JObject.Parse 与 JsonConvert.DeserializeObject

                  相关文章

                  最新文章

                  <legend id='J6bEx'><style id='J6bEx'><dir id='J6bEx'><q id='J6bEx'></q></dir></style></legend>
                • <small id='J6bEx'></small><noframes id='J6bEx'>

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

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