【发布时间】:2019-09-24 10:21:44
【问题描述】:
我正在为 FlotChart 准备数据,并且在 data 下进行数组转换。这不应包括变量名。
输入数据格式参考: Data format reference
JSON 中的预期结果:
[
{
"label": "Series 1",
"color": "#0077ff",
"data": [
[ "a", 1 ],
[ "b", 2 ],
[ "c", 3 ]
]
},
{
"label": "Series 2",
"color": "#ffccaa",
"data": [
[ "aa", 11 ],
[ "bb", 22 ],
[ "cc", 33 ]
]
}
]
我用来创建 JSON 输出的代码。为清晰起见简化:
Public Class DataSeries
Public Property label As String
Public Property color As String
Public Property data As List(Of DataSet)
End Class
Public Class DataSet
Public Property x As String
Public Property y As Decimal
End Class
Public Sub TestChart()
Dim DataForJSON As New List(Of DataSeries)
Dim MyDataSeries = As DataSeries
MyDataSeries = New DataSeries
MyDataSeries.label = "Series 1"
MyDataSeries.color = "#0077ff"
MyDataSeries.data = New List(Of DataSet)
WykresBarDataSet.data.Add(New DataSet() With {.x = "a", .y = 1})
WykresBarDataSet.data.Add(New DataSet() With {.x = "b", .y = 2})
WykresBarDataSet.data.Add(New DataSet() With {.x = "c", .y = 3})
MyDataSeries.Add(WykresBarDataSet)
DataForJSON.Add(MyDataSeries)
MyDataSeries = New DataSeries
MyDataSeries.label = "Series 2"
MyDataSeries.color = "#ffccaa"
MyDataSeries.data = New List(Of DataSet)
WykresBarDataSet.data.Add(New DataSet() With {.x = "aa", .y = 11})
WykresBarDataSet.data.Add(New DataSet() With {.x = "bb", .y = 22})
WykresBarDataSet.data.Add(New DataSet() With {.x = "cc", .y = 32})
MyDataSeries.Add(WykresBarDataSet)
DataForJSON.Add(MyDataSeries)
Dim JSON_txt As String = Newtonsoft.Json.JsonConvert.SerializeObject(DataForJSON)
End Sub
【问题讨论】:
-
data数组里面有错,应该是"data": [{"a", 1},{"b", 2},{"c", 3},。 ..]
-
@CruleD Samle JSON 来自工作示例(当数据硬编码时)。将尝试对其进行测试,但这仍然没有告诉我如何从我的班级生成这样的 JSON。