Advertisement

Latest Post

Thursday, 8 September 2016

Generic method to convert JSON to list

Below is simple generic method to convert JSON response into object list

public List<T> GenericJSONToListConverter<T, C>(T t, dynamic c)
        {
            List<T> ResponseList = new List<T>();
            if (c.GetType() == typeof(Newtonsoft.Json.Linq.JObject))
            {
                T Response = c.ToObject<T>();
                ResponseList.Add(errorResponse);
            }
            else if (c.GetType() == typeof(Newtonsoft.Json.Linq.JArray))
            {
                ResponseList = c.ToObject<List<T>>();
            }

            return ResponseList;
        }

Government Jobs