将两个JToken合并为一个

将两个JToken合并为一个

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

问题描述

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

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"
  }]
}

感谢您的帮助!

这是我到目前为止尝试过的:

This is what I have tried so far:

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

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.

类似这样的东西:

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

推荐答案

您可以使用 JContainer.Merge(Object, JsonMergeSettings) 将一个JObject合并到另一个.请注意, JsonMergeSettings.MergeArrayHandling 可以控制数组的合并方式.从 MergeArrayHandling枚举文档中,可能的合并选项为:

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.

因此使用MergeArrayHandling.Concat进行合并,如下所示,其中allPagespageOne均为JContainer类型(或子类,例如JObject):

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;

给予:

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

使用Replace合并时给出:

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

如果变量的类型为JToken,则需要将其强制转换为JContainer. (不是容器的JSON原语不能合并.)

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 可以控制是合并还是忽略null值,根据需要.

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

07-30 11:48