W.Harr
W.Harr

Reputation: 303

JSON Deserializing Returns Null for Sub List

I'm attempting to deserialize the following json:

{"PatientNameID":{"ID":514,"Name":{"First":"Laura","Middle":"X","Last":"Coelho","Suffix":"","Full":"Laura X Coelho","Preferred":""}},"PatientNumber":"254","ChartNumber":"254","Gender":{"LookupType":"Gender","Code":"F","Description":"Female","Order":1,"Active":true,"AlternateCodes":null},"DOB":"4/9/1953","PhoneNumber":"3521029496","SSN":"*****0161"}

This is the class and subclasses into which I'm trying to deserialize the above JSON:

public class PatientList3
{
    public Pat PatientNameID { get; set; }
    public string PatientNumber { get; set; }
    public string ChartNumber { get; set; }
    public Gender2 Gender { get; set; }
    public string DOB { get; set; }
    public string PhoneNumber { get; set; }
    public string SSN { get; set; }
}

public class Pat
{
    public int ID { get; set; }
    public PtName Name { get; set; }
}

public class PtName
{
    public string First { get; set; }
    public string Middle { get; set; }
    public string Last { get; set; }
    public string Suffix { get; set; }
    public string Full { get; set; }
    public string Preferred { get; set; }
}

public class Gender2
{
    string LookupType { get; set; }
    string Code { get; set; }
    string Description { get; set; }
    int Order { get; set; }
    bool Active { get; set; }
    List<AlternateCodes> AlternateCodes { get; set; }
}

public class AlternateCodes
{
    string Code { get; set; }
    string Description { get; set; }
    string CodeSystem { get; set; }
    string CodeSystemName { get; set; }
}

Everything goes well when I deserialize it except all of the values in the Gender2 class are null.

I've referred to the following two posts for answers but nothing seems to be doing to trick.

Upvotes: 0

Views: 121

Answers (2)

FaizanHussainRabbani
FaizanHussainRabbani

Reputation: 3439

The problem is with access modifiers of properties Gender2 and AlternateCodes class, default access modifiers for properties is private. You should change it to:

public class Gender2
{
    public string LookupType { get; set; }
    public string Code { get; set; }
    public string Description { get; set; }
    public int Order { get; set; }
    public bool Active { get; set; }
    public List<AlternateCodes> AlternateCodes { get; set; }
}

public class AlternateCodes
{
    public string Code { get; set; }
    public string Description { get; set; }
    public string CodeSystem { get; set; }
    public string CodeSystemName { get; set; }
}

After setting properties to public it deserializes successfully:

enter image description here

Upvotes: 1

cleftheris
cleftheris

Reputation: 4849

Fix the properties on Gender2 & AlternateCodes they are not public! The deserializer will not be able to find your any of your properties so that is probably the reason this fails to populate.

Upvotes: 3

Related Questions