I am new to JSON. I am trying to deserialize a JSON object into a C# Object. I do not get an error. After deserialization, the C# object properties are all null.
The C# Object that I defined has all possible properties. But the JSON object does not have all of these properties.
This is my C# Object:
[DataContract]
internal class Name
{
private string strFamilyName = string.Empty;
private string strGivenName = string.Empty;
private string strMiddleName = string.Empty;
private string strHonorificPrefix = string.Empty;
private string strHonorificSuffix = string.Empty;
[DataMember]
public string familyName
{
get { return strFamilyName; }
set { strFamilyName = value; }
}
[DataMember]
public string givenName
{
get { return strGivenName; }
set { strGivenName = value; }
}
[DataMember]
public string middleName
{
get { return strMiddleName; }
set { strMiddleName = value; }
}
[DataMember]
public string honorificPrefix
{
get { return strHonorificPrefix; }
set { strHonorificPrefix = value; }
}
[DataMember]
public string honorificSuffix
{
get { return strHonorificSuffix; }
set { strHonorificSuffix = value; }
}
}
[DataContract]
internal class UserProperties
{
private Name objName = null;
private string strEmail = string.Empty;
private string strMobile = string.Empty;
private string strPager = string.Empty;
private string strDegree = string.Empty;
private string strRole = string.Empty;
private string strSpecialty = string.Empty;
private string strDepartment = string.Empty;
[DataMember]
public Name name
{
get { return objName; }
set { objName = value; }
}
[DataMember]
public string email
{
get { return strEmail; }
set { strEmail = value; }
}
[DataMember]
public string mobile
{
get { return strMobile; }
set { strMobile = value; }
}
[DataMember]
public string pager
{
get { return strPager; }
set { strPager = value; }
}
[DataMember]
public string degree
{
get { return strDegree; }
set { strDegree = value; }
}
[DataMember]
public string role
{
get { return strRole; }
set { strRole = value; }
}
[DataMember]
public string specialty
{
get { return strSpecialty; }
set { strSpecialty = value; }
}
[DataMember]
public string department
{
get { return strDepartment; }
set { strDepartment = value; }
}
}
[DataContract]
internal class CortextUser
{
private string strCUID = string.Empty;
private string strInviteStatus = string.Empty;
private bool bIsEnabled = false;
private UserProperties objUserProperties = null;
[DataMember]
public string cuid
{
get { return strCUID; }
set { strCUID = value; }
}
[DataMember]
public string invitationStatus
{
get { return strInviteStatus; }
set { strInviteStatus = value; }
}
[DataMember]
public bool isEnabled
{
get { return bIsEnabled; }
set { bIsEnabled = value; }
}
[DataMember]
public UserProperties userProperties
{
get { return objUserProperties; }
set { objUserProperties = value; }
}
}
This is the JSON object returned when I use the code:
HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
Stream resStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
System.Diagnostics.Debug.WriteLine("JSON form of User object: ");
System.Diagnostics.Debug.WriteLine(reader.ReadToEnd());
JSON form of User object:
[
{
"cuid":"1338857734894",
"invitationStatus":"invite_accepted",
"isEnabled":true,
"userProperties":
{
"name":
{
"familyName":"Smith",
"givenName":"John"
},
"email":"[email protected]",
"mobile":"(111) 333-4444",
"role":"Developer",
"specialty":"Development",
"department":"IS"
}
}
]
When I run this code:
HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
Stream resStream = resp.GetResponseStream();
UserResult = (CortextUser)jsonSerializer.ReadObject(resStream);
The UserResult
object has all of the properties set to their initialized properties.
Do all properties need to be present in the JSON object when it is deserialized to the C# object?
Putting your JSON into https://app.quicktype.io/ results in these classes:
public class Name
{
public string familyName { get; set; }
public string givenName { get; set; }
}
public class UserProperties
{
public Name name { get; set; }
public string email { get; set; }
public string mobile { get; set; }
public string role { get; set; }
public string specialty { get; set; }
public string department { get; set; }
}
public class RootObject
{
public string cuid { get; set; }
public string invitationStatus { get; set; }
public bool isEnabled { get; set; }
public UserProperties userProperties { get; set; }
}
Once you have the JSON as a string you can deserialize it using JSON.NET with:
var root = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(json);
You need to deserialize into an IEnumerable
because your JSON is enclosed in square brackets (i.e. [ ... ]) which means it's a list/array/collection.
Here's a fiddle showing it working https://dotnetfiddle.net/hTM7EQ.