get,deserialized,json,object,c#

Osman 8/7/2016 0

get deserialized json object

C#
var url = "this-is-a-json-url" ;
var movies = _download_serialized_json_data<Movie>(url);


 private static List<Movie> _download_serialized_json_data<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();
                //return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<List<T>>(json_data) : new List<T>();

                var movies = JObject.Parse(json_data).SelectToken("results" ).ToString();

                return JsonConvert.DeserializeObject<List <Movie >>(movies);
            }
        } 
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments