【发布时间】:2014-08-11 15:47:06
【问题描述】:
我正在使用 Json.net 为 Flot Javascript 库创建数据结构
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);
}
问题似乎是 JSon.Net 不允许在 JObject 中直接使用 JArray,所以我添加了一个 JProperty 但与 Flot 库所需的结构不匹配
"": [
{
"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
],
]
}
]
问题:我是使用不同的方法来生成数据结构还是可以使用 JSON.Net 来生成?
【问题讨论】:
-
如果你想要一个数组,那为什么不直接使用
JArray retVal = new JArray(from x呢?还是我在这里误解了什么? -
感谢 cmets/edits - 我没有意识到我可以直接使用 JArray。