AnalyzeJSON.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. namespace analyzeJSON
  7. {
  8. public class AnalyzeJSON
  9. {
  10. private readonly JObject json;
  11. public AnalyzeJSON(string path)
  12. {
  13. if (!File.Exists(path))
  14. throw new ArgumentNullException("File doesn't exist!");
  15. using StreamReader sr = new StreamReader(path);
  16. var jsonString = sr.ReadToEnd();
  17. json = JsonConvert.DeserializeObject<JObject>(jsonString);
  18. }
  19. public static string GetNameFromPath(string tokenPath)
  20. {
  21. return tokenPath.Split(".").Last();
  22. }
  23. private void Traverse(IJEnumerable<JToken> tokens, Action<JToken> action)
  24. {
  25. foreach (var token in tokens)
  26. {
  27. action.Invoke(token);
  28. if (token.HasValues)
  29. Traverse(token.Children(), action);
  30. }
  31. }
  32. public void Traverse(Action<JToken> action)
  33. {
  34. if (!json.HasValues)
  35. return;
  36. Traverse(json.Children(), action);
  37. }
  38. }
  39. }