JSON 反序列化未设置 C# 对象

JSON Deserialization is not setting C# Object

我是 JSON 的新手。我正在尝试将 JSON 对象反序列化为 C# 对象。我没有收到错误。反序列化后,C#对象属性全部为null。 我定义的 C# 对象具有所有可能的属性。但是 JSON 对象并不具备所有这些属性。
这是我的 C# 对象:

[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; }
}       
}

这是我使用代码时返回的JSON对象:

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":"jsmith@test.org",
            "mobile":"(111) 333-4444",
            "role":"Developer",
            "specialty":"Development",
            "department":"IS"
        }
    }
]

当我运行这段代码时:

HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
Stream resStream = resp.GetResponseStream();
UserResult = (CortextUser)jsonSerializer.ReadObject(resStream);

UserResult 对象的所有属性都设置为它们的初始化属性。

当反序列化为 C# 对象时,JSON 对象中是否需要存在所有属性?

将您的 JSON 放入 https://app.quicktype.io/ 会导致这些 类:

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

将 JSON 作为字符串后,您可以使用 JSON.NET 反序列化它:

var root = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(json);

您需要反序列化为 IEnumerable,因为您的 JSON 包含在方括号中(即 [ ... ]),这意味着它是 list/array/collection.

这里有一个 fiddle 显示它正在工作 https://dotnetfiddle.net/hTM7EQ

尝试过

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

使用 Newtwonsoft Json 库并获取有效对象