2016-02-05 163 views
2

這裏是調用DeserializeObjectJson DeserializeObject返回null - 我做錯了什麼?

listrootobject obj = JsonConvert.DeserializeObject<listrootobject>(json); 

這裏是JSON。

{ 
    "so:MemberProfileDataSet": { 
     "@xmlns:moso": "http://schema.socloud.com/1/MemberProfileDataSet.xsd", 
     "Member": { 
      "@xmlns": "http://schema.socloud.com/1/MemberProfileDataSet.xsd", 
      "PartyId": "63", 
      "PartyTypeName": "Employee", 
      "RoleId": "1310", 
      "BusinessUnitCode": "95", 
      "CardId": null, 
      "PostalContact": { 
       "PartyId": "63", 
       "TypeName": "Mailing Address", 
       "Address1": "100 Main Ave", 
       "Address2": null, 
       "Address3": null, 
       "City": "Arlington", 
       "State": "VA", 
       "PostalCode": "22206" 
      }, 
      "PhoneContact": [{ 
       "PartyId": "63", 
       "TypeName": "Cell", 
       "CountryCode": "1", 
       "Telephone": "(555) 555-5555", 
       "AdditionalData": null, 
       "TextMessageOK": "false" 
      }, { 
       "PartyId": "63", 
       "TypeName": "Home", 
       "CountryCode": "1", 
       "Telephone": null, 
       "AdditionalData": null, 
       "TextMessageOK": "false" 
      }], 
      "Property": [{ 
       "PartyId": "63", 
       "Name": "First Name", 
       "Value": "Katie" 
      }, { 
       "PartyId": "63", 
       "Name": "Last Name", 
       "Value": "Rodriguez" 
      }, { 
       "PartyId": "63", 
       "Name": "Payroll ID", 
       "Value": null 
      }, { 
       "PartyId": "63", 
       "Name": "Lookup Initials", 
       "Value": null 
      }, { 
       "PartyId": "63", 
       "Name": "Date of Birth", 
       "Value": null 
      }, { 
       "PartyId": "63", 
       "Name": "Hire Date", 
       "Value": null 
      }, { 
       "PartyId": "63", 
       "Name": "Termination Date", 
       "Value": null 
      }, { 
       "PartyId": "63", 
       "Name": "Gender", 
       "Value": null 
      }] 
     } 
    } 
} 

這裏是C#類

public class PostalContact 
{ 
    public string PartyId { get; set; } 
    public string TypeName { get; set; } 
    public string Address1 { get; set; } 
    public string Address2 { get; set; } 
    public string Address3 { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string PostalCode { get; set; } 
} 

public class EmailContact 
{ 
    public string PartyId { get; set; } 
    public string TypeName { get; set; } 
    public string EmailAddress { get; set; } 
    public string EmailOptOut { get; set; } 
} 

public class PhoneContact 
{ 
    public string PartyId { get; set; } 
    public string TypeName { get; set; } 
    public string CountryCode { get; set; } 
    public string Telephone { get; set; } 
    public string AdditionalData { get; set; } 
    public string TextMessageOK { get; set; } 
} 

public class Property 
{ 
    public string PartyId { get; set; } 
    public string Name { get; set; } 
    public string Value { get; set; } 
} 

//public class rootobject 
//{ 
// public List<Member> Members { get; set; } 
//} 
public class Member 
{ 
    public string PartyId { get; set; } 
    public string PartyTypeName { get; set; } 
    public string RoleId { get; set; } 
    public string BusinessUnitCode { get; set; } 
    public string CardId { get; set; } 
    public PostalContact PostalContact { get; set; } 
    public EmailContact EmailContact { get; set; } 
    public List<PhoneContact> PhoneContact { get; set; } 
    public List<Property> Property { get; set; } 
    public string _xmlns { get; set; } 
} 

public class MemberProfileDataSet 
{ 
    public Member Member { get; set; } 
    public string __invalid_name___xmlns_moso { get; set; } 
    public string __prefix { get; set; } 
} 


public class listrootobject 
{ 
    public List<MemberProfileDataSet> rootobjects { get; set; } 
} 
public class rootobject 
{ 
    public MemberProfileDataSet MemberProfileDataSet { get; set; } 
} 
+0

你沒注意到你生成的類之後就再也沒有無效的屬性名稱有一個屬性名爲''__invalid_name___xmlns_moso?這顯然是錯誤的。 '因此:MemberProfileDataSet'不是'C#'中的有效成員名稱,所以反序列化包含對象的最好方法是將其反序列化爲一個'Dictionary '' – Rob

+0

@rob你是對的那些無效的東西搞亂了它。 – Amrik

回答

3

使用http://json2csharp.com/生成和寫你的C#類從網站生成的類。最後你解析rootObject如下。

但是你有一個問題,因爲它生成的屬性名稱在C#中是無效的,你不能用這些名稱創建一個C#類。但是這些名稱必須匹配才能反序列化。因此,您需要刪除無效字符或重新命名它們以符合C#CLR命名要求。然後,創建一個具有正確格式字符串的C#類。之後你可以反序列化。

比如我生成的C#對象從原來的字符串和我有一個屬性名作爲
public SoMemberProfileDataSet __invalid_name__so:MemberProfileDataSet { get; set; }

這是在C#中無效,但你需要的字符串格式化這個東西有效例如

public SoMemberProfileDataSet MemberProfileDataSet { get; set; }

通過重命名JSON字符串或從字符串得到你想要的屬性和正確重新構建JSON序列化德之前。

我顯示的代碼

作爲一個例子,我已經聯合冒號之間的字符串,並且使得我創建了一個有效的C#對象除去@符號。並且它去分散。請參閱下面的json字符串中的更改。所以請格式化你的json字符串,你會擁有它。

string json = "{\"soMemberProfileDataSet\": {\"xmlnsmoso\": \"http://schema.socloud.com/1/MemberProfileDataSet.xsd\",\"Member\": {\"xmlns\": \"http://schema.socloud.com/1/MemberProfileDataSet.xsd\",\"PartyId\": \"63\",\"PartyTypeName\": \"Employee\",\"RoleId\": \"1310\",\"BusinessUnitCode\": \"95\",\"CardId\": null,\"PostalContact\": {\"PartyId\": \"63\",\"TypeName\": \"Mailing Address\",\"Address1\": \"100 Main Ave\",\"Address2\": null,\"Address3\": null,\"City\": \"Arlington\",\"State\": \"VA\",\"PostalCode\": \"22206\"},\"PhoneContact\": [{\"PartyId\": \"63\",\"TypeName\": \"Cell\",\"CountryCode\": \"1\",\"Telephone\": \"(555) 555-5555\",\"AdditionalData\": null,\"TextMessageOK\": \"false\"}, {\"PartyId\": \"63\",\"TypeName\": \"Home\",\"CountryCode\": \"1\",\"Telephone\": null,\"AdditionalData\": null,\"TextMessageOK\": \"false\"}],\"Property\": [{\"PartyId\": \"63\",\"Name\": \"First Name\",\"Value\": \"Katie\"}, {\"PartyId\": \"63\",\"Name\": \"Last Name\",\"Value\": \"Rodriguez\"}, {\"PartyId\": \"63\",\"Name\": \"Payroll ID\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Lookup Initials\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Date of Birth\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Hire Date\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Termination Date\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Gender\",\"Value\": null}]}}}"; 

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

您的類現在看起來應該如下,格式化

public class PostalContact 
{ 
    public string PartyId { get; set; } 
    public string TypeName { get; set; } 
    public string Address1 { get; set; } 
    public object Address2 { get; set; } 
    public object Address3 { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public string PostalCode { get; set; } 
} 

public class PhoneContact 
{ 
    public string PartyId { get; set; } 
    public string TypeName { get; set; } 
    public string CountryCode { get; set; } 
    public string Telephone { get; set; } 
    public object AdditionalData { get; set; } 
    public string TextMessageOK { get; set; } 
} 

public class Property 
{ 
    public string PartyId { get; set; } 
    public string Name { get; set; } 
    public string Value { get; set; } 
} 

public class Member 
{ 
    public string xmlns { get; set; } 
    public string PartyId { get; set; } 
    public string PartyTypeName { get; set; } 
    public string RoleId { get; set; } 
    public string BusinessUnitCode { get; set; } 
    public object CardId { get; set; } 
    public PostalContact PostalContact { get; set; } 
    public List<PhoneContact> PhoneContact { get; set; } 
    public List<Property> Property { get; set; } 
} 

public class SoMemberProfileDataSet 
{ 
    public string xmlnsmoso { get; set; } 
    public Member Member { get; set; } 
} 

public class RootObject 
{ 
    public SoMemberProfileDataSet soMemberProfileDataSet { get; set; } 
} 
+0

去試試吧。保持聯繫。 – Amrik

+0

剛剛爲您添加的示例 –

+0

如您所見,我格式化了json字符串。所以,如果你動態地收到這個字符串通過api,當你收到它時,首先格式化它,然後編寫一些代碼,在序列化之前總是先格式化它。我在這裏手動格式化它只是爲了解釋它。 –