Reputation: 12821
I have a some data that I need serialized to json. Below is a simplified version of the code I'm using on the backend to generate the json:
public class RawBarData
{
public string Month { get; set; }
public decimal Total { get; set; }
}
List<RawBarData> lstData == getData();
string jdata = JsonConvert.SerializedObject(lstData);
After this runs, jdata looks something like this:
[
{ Month: "January", Total: 10},
{ Month: "February", Total: 8},
{ Month: "March", Total: 4},
{ Month: "April", Total: 13},
{ Month: "May", Total: 17},
{ Month: "June", Total: 9}
]
However I need the output to look like this:
[
["January", 10],
["February", 8],
["March", 4],
["April", 13],
["May", 17],
["June", 9]
]
How can I guide json.net to serialize the data in this format?
Upvotes: 0
Views: 101
Reputation: 86064
You can do it like this:
List<object[]> converted = getData()
.Select(r => new object[] { r.Month, r.Total })
.ToList();
string jdata = JsonConvert.SerializedObject(converted);
This replaces the RawBarData
instances with object
arrays, which will serialize into... you guessed it, JSON arrays.
Upvotes: 3