如何将这两个 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
合并如下,其中 allPages
和 pageOne
都是 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 合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!