是否有可能用JSON.NET从JSON对象中删除JSON对象?

本文关键字:JSON 对象 删除 NET 有可能 是否 | 更新日期: 2023-09-27 17:52:42

我可以在JSON中合并JSON对象。净但;

我想从JSON对象中删除JSON对象,像这样:

{
"name":"Mike",
"surname":"Dow",
"children":["James","John"],
"data":{"A1":"B1","A2":"B2","A3":"B3"},
"data2":{"A1":"B1","A2":"B2","A3":"B3","A4":"B4"},
"data3":{"A1":{"B1":"C1","B2":"C2"}}
}

-

{
"name":"Mike",
"children":["James"],
"data":{"A1":"B1"},
"data2":{"A3":"B3","A4":"B4"},
"data3":{"A1":{"B2":"C2"}}
}

=

{
"surname":"Dow",
"children":["John"],
"data":{"A2":"B2","A3":"B3"},
"data2":{"A1":"B1","A2":"B2"},
"data3":{"A1":{"B1":"C1"}}
}

是可能的JSON.NET?

是否有可能用JSON.NET从JSON对象中删除JSON对象?

您没有指定如何处理数组匹配。如果您只是匹配数组索引,那么您可以将JToken.SelectTokenJToken.Path组合起来,以匹配减号中的值与减号中的值:

        var minuend = JToken.Parse(minuendJson);
        var subtrahend = JToken.Parse(subtrahendJson);
        foreach (var toRemove in subtrahend.DescendantsAndSelf().OfType<JValue>().Select(t => minuend.SelectToken(t.Path)).Where(t => t != null).ToList())
            toRemove.RemoveFromLowestPossibleParent();

使用扩展方法

public static class JsonExtensions
{
    public static void RemoveFromLowestPossibleParent(this JToken node)
    {
        if (node == null)
            throw new ArgumentNullException();
        var contained = node.AncestorsAndSelf().Where(t => t.Parent is JArray || t.Parent is JObject).FirstOrDefault();
        if (contained != null)
            contained.Remove();
    }
    public static IEnumerable<JToken> DescendantsAndSelf(this JToken node)
    {
        if (node == null)
            return Enumerable.Empty<JToken>();
        var container = node as JContainer;
        if (container != null)
            return container.DescendantsAndSelf();
        else
            return new [] { node };
    }
}

如果您想按值而不是按索引匹配"leaf"数组的值,您可以这样做:

public static class JsonExtensions
{
    public static JToken MatchToken(this JToken target, JToken source)
    {
        var sourceArray = source.Parent as JArray;
        if (sourceArray != null)
        {
            var targetArray = target.SelectToken(sourceArray.Path) as JArray;
            if (targetArray != null)
            {
                // There may be duplicated values in the source and target arrays. If so, get the relative index of the
                // incoming value in the list of duplicates in the source, and return the corresponding value in the list
                // of duplicates in the target.
                var sourceIndices = Enumerable.Range(0, sourceArray.Count).Where(i => JToken.DeepEquals(sourceArray[i], source)).ToList();
                var targetIndices = Enumerable.Range(0, targetArray.Count).Where(i => JToken.DeepEquals(targetArray[i], source)).ToList();
                var matchIndex = sourceIndices.IndexOf(sourceArray.IndexOf(source));
                Debug.Assert(matchIndex >= 0);// Should be found inside its parent.
                if (matchIndex >= 0 && matchIndex < targetIndices.Count)
                    return targetArray[targetIndices[matchIndex]];
                return null;
            }
        }
        return target.SelectToken(source.Path);
    }
}

        var minuend = JToken.Parse(minuendJson);
        var subtrahend = JToken.Parse(subtrahendJson);
        foreach (var toRemove in subtrahend.DescendantsAndSelf().OfType<JValue>().Select(t => minuend.MatchToken(t)).Where(t => t != null).ToList())
            toRemove.RemoveFromLowestPossibleParent();