简体   繁体   English

C#JObject没有正确嵌套

[英]C# JObject not nesting properly

I'm dealing with some issues merging my json arrays. 我正在处理合并我的json数组的一些问题。 The original arrays to be merged look similar to this: 要合并的原始数组看起来类似于:

{
  "ads":[
    {
      "a1": "b1",
      "i1": "j1",
      "x1": "y1",
    }
  ]
}

{
  "ads": [
    {
      "a2": "b2",
      "i2": "j2",
      "x2": "y2",
    }
  ]
}

The result of my merge looks similar to this: 我合并的结果看起来类似于:

{
  "ads:[
    [
      {
        "a1": "b1",
        "i1": "j1",
        "x1": "y1",
      }
    ],
    [
      {
        "a2": "b2",
        "i2": "j2",
        "x2": "y2",
      }
    ]
  ]
}

What I am trying to pare this down to is: 我想要削减的是:

{
  "ads:[
    {
      "a1": "b1",
      "i1": "j1",
      "x1": "y1",
    },
    {
      "a2": "b2",
      "i2": "j2",
      "x2": "y2",
    }
  ]
}

The code that is being used to attempt to merge the two is currently this: 用于尝试合并两者的代码目前是这样的:

        // Combine all ads within formattedContent into one single JOBject.
        JToken token = null;
        JArray jarray = new JArray();
        List<JToken> jtokens = new List<JToken>();
        foreach (JObject jobject in formattedContent)
        {
            token = JToken.Parse(jobject.SelectToken("ads").ToString());                
            jarray.Add(token);
        }

But I am stuck with the doubly nested arrays when the end product is needs to result in all ads in the "ads" token under one array. 但是当最终产品需要在一个阵列下的“ads”标记中产生所有广告时,我会被双嵌套数组所困扰。 Note that formattedContent in the above code is a List<JObject> . 请注意,上面代码中的formattedContent是List<JObject> Any help in merging json arrays would be appreciated. 任何合并json数组的帮助都将受到赞赏。 Attempting to add the tokens to a string before adding them to the JArray results in carriage returns and escape characters being added in. (These are not being added while only in the debugger view [as I have seen frequently suggested], they are actually being reflected in the end product) 在将标记添加到JArray之前尝试将标记添加到字符串会导致添加回车符和转义字符。(这些不是仅在调试器视图中添加[正如我经常看到的那样],它们实际上是反映在最终产品中)

What you want to do is to add token.Children() rather than token to jarray (ie add the contents of token rather than token itself): 你想要做的是将token.Children()而不是token添加到jarray (即添加token的内容而不是token本身):

var jarray = new JArray();
foreach (var jobject in formattedContent)
{
    var token = jobject.SelectToken("ads") as JArray;
    if (token != null)
        jarray.Add(token.Children());
}

Incidentally, you could achieve the same effect using JContainer.Merge() on the root object, with the setting JsonMergeSettings.MergeArrayHandling = MergeArrayHandling.Concat : 顺便说一下,你可以使用根对象上的JContainer.Merge()来实现相同的效果,设置JsonMergeSettings.MergeArrayHandling = MergeArrayHandling.Concat

var merged = new JObject();
var settings = new JsonMergeSettings
{
    MergeArrayHandling = MergeArrayHandling.Concat,
};
foreach (var jobject in formattedContent)
{
    merged.Merge(jobject, settings);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM