2016-02-05 74 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,当你收到它时,首先格式化它,然后编写一些代码,在序列化之前总是先格式化它。我在这里手动格式化它只是为了解释它。 –