0

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?

2
  • 2
    Please show the actual JSON returned rather than how you returned it. Ideally, provide this as a minimal reproducible example. Commented Jan 26, 2017 at 16:56
  • 2
    You just need to remove new[] for location key because LINQ expression ends with .ToList() already returns a list of items. Commented Jan 26, 2017 at 17:14

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the answer and yes you are right.Once again thank you so much and please keep helping like this :)
1

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()

Comments

1

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()
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.