2015-06-01 56 views
0

我可以在JSON.NET中合併JSON對象,但是;是否可以使用JSON.NET從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?

+0

可能的重複http://stackoverflow.com/questions/11676159/json-net-how-to-remove-nodes – Coder1409

+0

它不是重複的。我不想刪除一個數組的屬性,但一個JSON。我想要JTokens的區別。 – izel

+0

這正是在另一篇文章中所做的,只需要一些定製在你的結束! – Coder1409

回答

0

您不指定要如何處理數組匹配。如果你簡單地匹配數組索引,那麼你就可以JToken.Path結合JToken.SelectToken與在被減數值匹配的減數值:

 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 }; 
    } 
} 

如果你想匹配按價值計算,而不是按索引「葉子」數組的值,你可以做這樣的事情:

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();