0

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?

6
  • You Json Appears to be an array of the UserResult Type. Take a look at this, stackoverflow.com/questions/16856846/… Serialize it to a list and grab your first result based upon your example. Commented Nov 25, 2015 at 16:58
  • No. It is not an array of UserResult type. The UserResult object has nested objects, ie UserProperties and UserProperties has a Name object. Commented Nov 25, 2015 at 17:00
  • Yes and i can see User properties defined in the object above but at a top level your entire response is wrapped in [ ] which means start of an array. yours just happens to be an array of 1. Commented Nov 25, 2015 at 17:02
  • Thank you. You are correct. I will look into how to deserialize an array. Commented Nov 25, 2015 at 17:05
  • Once you serialized to a list you can simply call first on it and you should get your UserResult Object you need, however I would probably do a check on the list count to make sure you don't get back more than 1 object. :) hope that works for you! Commented Nov 25, 2015 at 17:06

2 Answers 2

2

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.

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

Comments

0

Tried

CortextUser user = JsonConvert.DeserializeObject<CortextUser>(text);

With Newtwonsoft Json library and get a valid object

1 Comment

IT is an array, so I had to add <IEnumerable> as listed in first answer.

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.