I would like to use JavaScriptSerializer for this because I'm afraid of how many things might break and I can't change anything on the client-side.
If Json.Net is the best way to go then I will try it but I need an example.
I have this class
Class Definition
[DataContract]
[Serializable]
public class Family
{
[DataMember(Order = 0)]
public List<Member> members { get; set; }
}
[DataContract]
[Serializable]
public class Member
{
[DataMember(Order = 0)]
public string FName { get; set; }
[DataMember(Order = 1)]
public string LName { get; set; }
[DataMember(Order = 2)]
public string DOB { get; set; }
[DataMember(Order = 3)]
public string Gender { get; set; }
[DataMember(Order = 4)]
public string Type { get; set; }
}
The JSON I am Deserializing looks like this
JSON Example
[
{
"Family": [
{
"FName": "Jane",
"LName": "Prospect",
"DOB": "04/01/1980",
"Gender": "Female",
"Type": "Adult"
},
{...}
]
},
{
"OptionChoice": 34,
"OptionText": "Aquatics"
},
{...},
{...}
]
I can deserialize the Answer objects fine (OptionChoice,OptionText). Hower the answer object has an additional item full of nulls where it is parsing the Family section of the JSON. I don't really want that.
When I try to deserialize the Family part I get an error
Type 'Family' is not supported for deserialization of an array.
It says it has a null for the Family.members. Is it looking for "Family": [ "members": {...},{...}] ?
How can I get this working without changing the JSON example?
Update After dbc Answer:
Here is my model
[DataContract]
[Serializable]
public class Answer
{
[DataMember(Order = 2, EmitDefaultValue = false)]
public int FormID { get; set; }
[DataMember(Order = 3,EmitDefaultValue = false)]
public int Question { get; set; }
[DataMember(Order = 5)]
public int OptionChoice { get; set; }
[DataMember(Order = 6,IsRequired = false)]
public string OptionText { get; set; }
[DataMember(Order = 5, EmitDefaultValue = false)]
public bool lockAnswer { get; set; }
[DataMember(Order = 1,EmitDefaultValue= false)]
public List<FamilyMember> Family { get; set; }
}
[DataContract]
[Serializable]
public class FamilyMember
{
[DataMember(Order = 0)]
public string FName { get; set; }
[DataMember(Order = 1)]
public string LName { get; set; }
[DataMember(Order = 2)]
public string DOB { get; set; }
[DataMember(Order = 3)]
public string Gender { get; set; }
[DataMember(Order = 4)]
public string Type { get; set; }
}
At this point in my creation of my SO update I solved the problem with Rubber Duck Debugging
Turns out I had some regex that was stripping all "[" & "]" then manually adding them back but only on the ends.
This cool comparison tool at http://pro.jsonlint.com/ helped btw.
Thanks! If anybody in the future reads this and wants to show a testable way to do this in JSON.net (I tried but got stuck) please do.