【问题标题】:Serialise object to JSON with array - preparing data for bar Flot Chart使用数组将对象序列化为 JSON - 为条形图准备数据
【发布时间】: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。

标签: json vb.net json.net


【解决方案1】:

与:

Dim DataForJSON As New List(Of DataSeries)

Dim MyDataSeries As New DataSeries With {
    .label = "Series 1",
    .color = "#0077ff",
    .data = New List(Of DataSet)
}
MyDataSeries.data.Add(New DataSet With {.x = "a", .y = 1})
MyDataSeries.data.Add(New DataSet With {.x = "b", .y = 2})
MyDataSeries.data.Add(New DataSet With {.x = "c", .y = 3})
DataForJSON.Add(MyDataSeries)

MyDataSeries = New DataSeries With {
    .label = "Series 2",
    .color = "#ffccaa",
    .data = New List(Of DataSet)
}
MyDataSeries.data.Add(New DataSet With {.x = "aa", .y = 11})
MyDataSeries.data.Add(New DataSet With {.x = "bb", .y = 22})
MyDataSeries.data.Add(New DataSet With {.x = "cc", .y = 32})
DataForJSON.Add(MyDataSeries)

Dim JSON_txt As String = Newtonsoft.Json.JsonConvert.SerializeObject(DataForJSON)

输出:

[
   {"label":"Series 1",
    "color":"#0077ff",
    "data":[
         {"x":"a", "y":1.0},
         {"x":"b", "y":2.0},
         {"x":"c", "y":3.0}
           ]
   },
   {"label":"Series 2",
    "color":"#ffccaa",
    "data":[
         {"x":"aa", "y":11.0},
         {"x":"bb", "y":22.0},
         {"x":"cc", "y":32.0}
           ]
   }
]

哪个是正确的输出。


或者如果你想要

 "data": [
      [ "aa", 11 ],
      [ "bb", 22 ],
      [ "cc", 33 ]

然后这样做:

   Public Class DataSeries
        Public Property label As String
        Public Property color As String
        Public Property data As List(Of List(Of Object))
    End Class

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim DataForJSON As New List(Of DataSeries)

        Dim MyDataSeries As New DataSeries With {
            .label = "Series 1",
            .color = "#0077ff",
            .data = List(Of List(Of Object))
        }
        MyDataSeries.data.Add(New List(Of Object)({"aa", 11}))
        MyDataSeries.data.Add(New List(Of Object)({"bb", 22}))
        MyDataSeries.data.Add(New List(Of Object)({"cc", 32}))
        DataForJSON.Add(MyDataSeries)

        Dim JSON_txt As String = Newtonsoft.Json.JsonConvert.SerializeObject(DataForJSON)

    End Sub

输出:

[
   {"label":"Series 1",
    "color":"#0077ff",
    "data":[
         ["aa", 11],
         ["bb", 22],
         ["cc", 32]
           ]
   }
]

【讨论】:

  • 提供的 JSON 来自工作但静态的示例。我正在尝试动态创建它。除了数组格式问题,如何去掉x和y变量名?
  • 不要做属性,属性就是名字,属性的值就是值。要获取内部数组和数组,可以将列表添加到列表中。
  • 我已经更新了问题,链接到 Flot Charts 的数据格式参考。
  • 我还添加了第二部分。
  • 快到了。现在唯一的问题是数字应该是数字而不是字符串[ "aa", 11 ] 而不是[ "aa", "11" ]。图表库使用这些数字进行计算并正确显示在 y 轴上。所以一个字符串和十进制对列表的列表。
猜你喜欢
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 2021-05-29
  • 2011-12-19
相关资源
最近更新 更多