AnalyzeStructure.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Collections.Generic;
  2. using Newtonsoft.Json.Linq;
  3. namespace analyzeJSON
  4. {
  5. public class AnalyzeStructure
  6. {
  7. private Dictionary<string, int> nodes = new Dictionary<string, int>();
  8. private Dictionary<string, int> leafs = new Dictionary<string, int>();
  9. public AnalyzeStructure()
  10. {
  11. }
  12. public void AnalyzeToken(JToken token)
  13. {
  14. if (token.Type == JTokenType.Object || token.Type == JTokenType.Array)
  15. return;
  16. var tokenName = AnalyzeJSON.GetNameFromPath(token.Path);
  17. if (token.HasValues)
  18. {
  19. if (token.First.Equals(token.Last) &&
  20. token.First.Type != JTokenType.Array && token.First.Type != JTokenType.Property && token.First.Type != JTokenType.Object)
  21. {
  22. return;
  23. }
  24. if (nodes.ContainsKey(tokenName))
  25. nodes[tokenName]++;
  26. else
  27. nodes.Add(tokenName, 1);
  28. }
  29. else
  30. {
  31. if (leafs.ContainsKey(tokenName))
  32. leafs[tokenName]++;
  33. else
  34. leafs.Add(tokenName, 1);
  35. }
  36. }
  37. }
  38. }