I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

Unable to deserialize JSON array

Below is my class :

public class Employee : Base
{
    public int Id { get; set; }
    public string Fname { get; set; } 
    public DepartmentModel Department { get; set; }
}

public class DepartmentModel : Base
{
    public int Id { get; set; }
    public string DepartmentName { get; set; }
    public List<Location> Locations { get; set; }
}

public class Locations
{
    public string Area { get; set; }
    public string StreetNo { get; set; }
    public string Nearby { get; set; }
}

Response return from service:

var response = new
{
    id = 100,
    department = new
    {
        id = 200,
        departmentName = "Abc",
        locations = new[]
        {
            Employee.Department.Locations
                    .Select
                    (
                        lo => new
                        {
                             area = lo.Area,
                             streetNo = lo.streetNo,
                             nearby = lo.Nearby
                        }
                    ).ToList()
        }
    }
};

return JsonConvert.SerializeObject(response);

Now when I try to deserialize this above JSON into my class Employee like below:

var deserialize = JsonConvert.DeserializeObject<Employee>(response.ToString());

Error:

enter image description here

How can I deserialize this above JSON?

Upvotes: 0

Views: 255

Answers (3)

absynce
absynce

Reputation: 1437

You need to instantiate a new Employee() and use the same casing as the classes:

var response = new Employee() // Instantiates Employee to ensure correct properties used.
{
    Id = 100, // Has PascalCase.
    Department = new DepartmentModel()
    {
        Id = 200, 
        DepartmentName = "Abc",
        Locations = Employee.Department.Locations
                            .Select(lo => new Location
                                       {
                                             Area = lo.Area,
                                             StreetNo = lo.StreetNo,
                                             Nearby = lo.Nearby
                                        }
                                    ).ToList()
    }
};

Upvotes: 1

Jonathan Gilbert
Jonathan Gilbert

Reputation: 3840

The problem lies here:

locations = new[]
            {
                Employee.Department.Locations
                        .Select
                        (
                            lo => new
                            {
                                area = lo.Area,
                                streetNo = lo.streetNo,
                                nearby = lo.Nearby
                            }
                        ).ToList()
            }

The LINQ expression ends with .ToList() and thus is already returning a list of items. You are then wrapping that with new[] in an array. So, instead of being an array of Locations, the JSON is an array of an array of Locations.

Upvotes: 3

Aducci
Aducci

Reputation: 26634

Try removing the new[]. You don't want locations to be an array of lists

locations = Employee.Department.Locations
                               .Select(lo => new
                                           {
                                                 area = lo.Area,
                                                 streetNo = lo.streetNo,
                                                 nearby = lo.Nearby
                                            }
                                        ).ToList()

Upvotes: 1

Related Questions