JSON.NET: how to serialize nested collections
This is driving me crazy ... I am serializing a list to JSON using Json.net. I am expecting this JSON:
{
"fieldsets": [
{
"properties": [
{
"alias": "date",
"value": "2014-02-12T00:00:00"
},
{
"alias": "time",
"value": null
}
],
"alias": "eventDates",
"disabled": false
}
]
}
But instead I get this:
{
"fieldsets": [
{
"properties": [
{
"values": [
{
"alias": "date",
"value": "2014-07-13T00:00:00"
},
{
"alias": "time",
"value": "Registration begins at 8:00 AM; walk begins at 9:00 AM"
}
]
}
],
"alias": "eventDates",
"disabled": false
}
]
}
A collection of "values" that I would like to use only as a JSON array, but I can't figure out for my life how to do that. I have a property on "property" objects called "values", so I understand why it does this, but I only want a straight array, not a JSON object.
+3
source to share
2 answers
For this answer you need this class structure
public class Property
{
[JsonProperty("alias")]
public string Alias { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public class Fieldset
{
[JsonProperty("properties")]
public Property[] Properties { get; set; }
[JsonProperty("alias")]
public string Alias { get; set; }
[JsonProperty("disabled")]
public bool Disabled { get; set; }
}
public class Response
{
[JsonProperty("fieldsets")]
public Fieldset[] Fieldsets { get; set; }
}
+8
source to share
This could be the answer:
public class Property
{
public string alias { get; set; }
public string value { get; set; }
}
public class Fieldset
{
public List<Property> properties { get; set; }
public string alias { get; set; }
public bool disabled { get; set; }
}
public class RootObject
{
public List<Fieldset> fieldsets { get; set; }
}
0
source to share