我是JSON的新手。我想將一個JSON對象反序列化成一個C#對象。我沒有收到錯誤。反序列化後,C#對象屬性全部爲空。 我定義的C#對象具有所有可能的屬性。但是JSON對象並不具備所有這些屬性。
這是我的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":"[email protected]",
"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看來是UserResult類型的數組。看看這個,http://stackoverflow.com/questions/16856846/deserialize-a-json-array-in-c-sharp將它序列化爲一個列表並根據您的示例獲取您的第一個結果。 – Bearcat9425
不是。它不是UserResult類型的數組。 UserResult對象具有嵌套的對象,即UserProperties和UserProperties具有一個Name對象。 –
是的,我可以看到上面的對象中定義的用戶屬性,但是在頂層,您的整個響應都封裝在[]中,這意味着數組的開始。你的數組恰好爲1. – Bearcat9425