I am using Json.net to create a data structure for Flot Javascript library
public ActionResult PrepareChartData(int id)
{
List<DeviceChannel> deviceChannel = (from dc in db.DeviceChannels.Include(c => c.Device)
.Include(c => c.Channel).OrderBy(c => c.ChannelID)
.Where(c => c.DeviceID == id)
select dc).ToList();
List<ChannelData> channelData = (from cd in db.ChannelDatas
join dc in db.DeviceChannels on cd.DeviceChannelID equals dc.DeviceChannelID
where cd.DeviceChannelID == id
select cd).ToList();
JObject retVal = new JObject(new JProperty("",
new JArray(
from x in deviceChannel
select new JObject (
new JProperty("label", x.Channel.Description),
new JProperty("data",
new JArray(
from cd in channelData
select new JArray(new JValue(cd.DataDateTime),new JValue(cd.Value))
)
)
)
)
)
);
return Json(retVal, JsonRequestBehavior.AllowGet);
}
The problem appears to be that JSon.Net does not allow a JArray directly inside a JObject, so I added a JProperty but that does not match the structure required by Flot library
"": [
{
"label": "Voltage A",
"data": [
[
"2014-06-25T14:24:38.41",
750.0
],
[
"2014-06-25T15:28:04.427",
750.0
],
[
"2014-06-25T16:29:13.757",
750.0
]
]
},
{
"label": "Voltage B",
"data": [
[
"2014-06-25T14:24:38.41",
750.0
],
[
"2014-06-25T15:28:04.427",
750.0
],
[
"2014-06-25T16:29:13.757",
750.0
],
]
}
]
Question: Do I use a different method to generate the data structure or can I do this with JSON.Net?