So here's what I am trying to do and what I have accomplished.
private static T _getjson<T>(string url) where T : new()
{
using (var w = new WebClient())
{
var json_data = string.Empty;
// attempt to download JSON data as a string
try
{
json_data = w.DownloadString(url);
}
catch (Exception) { }
// if string with JSON data is not empty, deserialize it to class and return its instance
return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T();
}
}
This method (when called) is used like so:
var onlineornot = ("http://blah.com");
var chatters = _getjson<Rootobject>(onlineornot);
<Rootobject> being a class set up like this:
public class Rootobject
{
public _Links _links { get; set; }
public int chatter_count { get; set; }
public Chatters chatters { get; set; }
public Stream stream { get; set; }
public Stream game { get; set; }
public _Links2 _links2 { get; set; }
}
For the most part, it works but it causes my app to hang every time I call _getJson. I was wondering how I could use Async in this case, while maintaining the ability to get the properties from <Rootobject>.
_getjsonThe framework design guidelines are coming to get you, Pat. And WebClient offers async methods. What's the problem with using them?