2015-11-25 31 views
0

我是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對象中?

+0

您的Json看來是UserResult類型的數組。看看這個,http://stackoverflow.com/questions/16856846/deserialize-a-json-array-in-c-sharp將它序列化爲一個列表並根據您的示例獲取您的第一個結果。 – Bearcat9425

+0

不是。它不是UserResult類型的數組。 UserResult對象具有嵌套的對象,即UserProperties和UserProperties具有一個Name對象。 –

+0

是的,我可以看到上面的對象中定義的用戶屬性,但是在頂層,您的整個響應都封裝在[]中,這意味着數組的開始。你的數組恰好爲1. – Bearcat9425

回答

1

把你的JSON到http://json2csharp.com導致這些類:

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被括在方括號中(即[...]),這意味着它是一個列表/數組/集合。

這是一個小提琴展示它的工作https://dotnetfiddle.net/hTM7EQ

+0

是的。這工作。 –

0

試圖

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

隨着Newtwonsoft JSON庫,並得到有效的對象

+0

它是一個數組,因此我必須添加第一個答案中列出的。 –