JsonDownloader.cs 866 B

1234567891011121314151617181920212223242526
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using Newtonsoft.Json;
  5. namespace EthermineBotTelegramCore
  6. {
  7. public class JsonDownloader
  8. {
  9. public static T _download_serialized_json_data<T>(string url) where T : new() {
  10. using (var w = new WebClient()) {
  11. var jsonData = string.Empty;
  12. // attempt to download JSON data as a string
  13. try
  14. {
  15. jsonData = w.DownloadString(url);
  16. jsonData = jsonData.Replace("null", "0");
  17. }
  18. catch (Exception) { }
  19. // if string with JSON data is not empty, deserialize it to class and return its instance
  20. return !string.IsNullOrEmpty(jsonData) ? JsonConvert.DeserializeObject<T>(jsonData) : new T();
  21. }
  22. }
  23. }
  24. }