2

Here is the call to DeserializeObject:

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

Here is 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
 }]
 }
 }
}

Here is C# Class

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; }
}
Rob
27.4k16 gold badges89 silver badges103 bronze badges
asked Feb 5, 2016 at 0:03
2
  • Did you not notice your generated classes has a property named __invalid_name___xmlns_moso ? That's clearly wrong. so:MemberProfileDataSet is not a valid member name in C#, so the best way to deserialize the containing object is to deserialize it into a Dictionary<string, object> Commented Feb 5, 2016 at 0:38
  • @rob you were right those invalid stuff messing with it. Commented Feb 5, 2016 at 2:05

1 Answer 1

3

Use http://json2csharp.com/ to generate and write your C# classes as the generated classes from the website. Lastly you parse the rootObject as below.

But you have a problem because the property names it generates are invalid in C# and you cannot create a C# class with those names. But the names must match to de-serialize. So you need to remove the invalid characters or re-name them to meet the C# CLR naming requirements.Then create a C# class of the properly formatted string. After which you can de-serialized.

For instance I generated C# object from the original string and I had a property name as
public SoMemberProfileDataSet __invalid_name__so:MemberProfileDataSet { get; set; }

This is invalid in C# but you would need to format this in the string to something valid e.g.

public SoMemberProfileDataSet MemberProfileDataSet { get; set; }

by renaming the json string or getting the properties you want from the string and re-constructing the json properly before de serializing.

Show me the code

As an example, I have combined strings between colons and removed the @ symbol so that I created a valid C# object. And it de-seralizes. Please see changes in the json string below. So please format your json string and you will have it.

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

Your class should now look like as below, no more invalid property names after formatting

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; }
}
answered Feb 5, 2016 at 0:42

4 Comments

going to try it. keep you posted.
Just added example for you
As you can see, I formatted the json string. So if you receive the string dynamically e.g. via api, when you receive it, first format it, write some code to always format it first before de serializing. I formatted it here manually just to explain it.
Or use Edit->Paste Special->Paste JSON as Classes if you're on VS 2012 or better. You, of course, need to copy the source JSON first.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.