This seems to be the same question as below but that answer hasn't resolved it:
Deserializing a Simple JSON Array
I am using DataContractJsonSerializer to convert from XML to JSON and vice versa. Everything works with complex data types and arrays of complex data types but I'm having a problem producing JSON for a string array.
The JSON I need to produce should have this structure:
{
"data": {
"x_axis": {
"labels": [ "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", Aug","Sep", Oct", "Nov", "Dec" ]
}
}
}
The objects I am using are:-
LineChartData:
[DataContract]
public class LineChartData
{
[DataMember(Name = "x_axis")]
public LineChartXAxis XAxis { get; set; }
}
LineChartXAxis:
[DataContract]
public class LineChartXAxis
{
[DataMember(Name = "labels")]
public string[] Labels { get; set; }
}
The XML I am trying to convert looks like this:
<LineChartData>
<XAxis>
<Labels>Jan</Labels>
<Labels>Feb</Labels>
<Labels>Mar</Labels>
<Labels>Apr</Labels>
<Labels>May</Labels>
<Labels>Jun</Labels>
<Labels>Jul</Labels>
<Labels>Aug</Labels>
<Labels>Sep</Labels>
<Labels>Oct</Labels>
<Labels>Nov</Labels>
<Labels>Dec</Labels>
</XAxis>
</LineChartData>
My deserialising code is:
var serialiser = new XmlSerializer(typeof(LineChartData));
var stringReader = new StringReader(xml);
var result = serialiser.Deserialize(stringReader);
The JSON I get back always has an empty labels array:
{
"data": {
"x_axis": {
"labels":[]
}
}
}
How should I be defining the Labels property in LineChartXAxis to serialise the JSON correctly?