JSON Array Deserializing to list RRS feed

  • Question

  • User1858009984 posted

    While consuming the Web API, how to deserailize JSON Array into generic list, i am getting null values.

    Monday, March 4, 2019 6:35 AM

All replies

  • User1120430333 posted

    The Json must match the public properties of a type,  a class, like a DTO as an example.


     public class WebApi : IWebApi
            #region ProjectApi
            public List<DtoProject> GetProjsByUserIdApi(string userid)
                var dtoprojects = new List<DtoProject>();
                using (var client = new HttpClient())
                    var uri = new Uri("http://progmgmntcore2api.com/api/project/GetProjsByUserId?userid=" + userid);
                    var response = client.GetAsync(uri).Result;
                    if (!response.IsSuccessStatusCode)
                        throw new Exception(response.ToString());
                    var responseContent = response.Content;
                    var responseString = responseContent.ReadAsStringAsync().Result;
                    dynamic projects = JArray.Parse(responseString) as JArray;
                    foreach (var obj in projects)
                        DtoProject dto = obj.ToObject<DtoProject>();

    return dtoprojects;

    using System;
    namespace Entities
        public class DtoProject
            public int ProjectId { get; set; }
            public string ClientName { get; set; }
            public string ProjectName { get; set; }
            public string Technology { get; set; }
            public string ProjectType { get; set; }
            public string UserId { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public decimal Cost { get; set; }

    Same thing in VB.NET but done a little differently where I didn't go into a loop on the dynamic done in C# and VB.NET doesn't have a ToObject(). I think I could have eliminated the loop in C# and did it like VB.NET but never got around to it for some reason. 

    Note: the DTO was sent back as a collection of DTO(s) coming back from the WebAPI in C# and VB.NET, which were Json serialized. The client project and the WebAPI project know about the DTO.

    public Function  GetProjsByUserIdApi(userid As String) as List(of DtoProject) Implements IWebApi.GetProjsByUserIdApi
                dim dtoprojects = new List(Of DtoProject)
                dim url = "http://localhost/WebApiVB/api/project/GetProjectsByUserId?userid=" & userid
                Using webclient As New WebClient
                    dim json  = webclient.DownloadString(url)
                    Dim projects = JsonConvert.DeserializeObject(of List(Of DtoProject))(json)
                    dtoprojects = projects
                End Using
                Return dtoprojects
            End Function
    Public Class DtoProject
        Public Property ProjectId As Int32
        Public Property ClientName As String
        Public Property ProjectName As String
        Public Property Technology As String
        Public Property ProjectType As String
        Public Property UserId As String
        Public Property StartDate  As DateTime
        Public Property EndDate As DateTime? 
        Public Property Cost As Decimal
    End Class

    Monday, March 4, 2019 7:17 AM
  • User36583972 posted

    Hi Ravind,

    While consuming the Web API, how to deserailize JSON Array into generic list, i am getting null values.

    When you receive a JSON Array respone from Web API, You can use the following way to deserailize JSON Array into generic list.

    1: Using the JavaScriptSerializer to deserialize the string into a native object.

    2: Using JSON.NET for dynamic JSON parsing

    Best Regards

    Yong Lu

    Tuesday, March 5, 2019 2:05 AM
  • User-1038772411 posted

    Hope This code works for You  :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Script.Serialization;
    namespace Json
        class Program
            static void Main(string[] args)
            public static string DeserializeNames()
                var jsonData = "{\"name\":[{\"last\":\"Smith\"},{\"last\":\"Doe\"}]}";
                JavaScriptSerializer ser = new JavaScriptSerializer();
                nameList myNames = ser.Deserialize<nameList>(jsonData);
                return ser.Serialize(myNames);
            //Class descriptions
            public class name
                public string last { get; set; }
            public class nameList
                public List<name> name { get; set; }

     I implemented this on my project its work fine for me. and i am sharing this link for your reference.

    Monday, March 11, 2019 1:43 PM