重命名VB.Net中的数据集

时间:2018-09-14 14:40:47

标签: vb.net

我有下面的代码(用VB.Net编写),试图在声明后DataSet重命名,但是它不起作用。数据集保持其通用名称(即“ Table”)。有人知道为什么不更改DataSet的名称吗?

Dim ds As New DataSet With {
        .DataSetName = "data"
    }

这是将DataSet结果转换为JSON的方式:

Dim myJson As String = JsonConvert.SerializeObject(ds, Formatting.Indented, New JsonSerializerSettings With { .ContractResolver = New CamelCasePropertyNamesContractResolver() })

但是,结果中包含的是“表格”而不是“数据”

{

  "table": [
  {
    "Column1": "John",
    "Column2": "49",
  },

2 个答案:

答案 0 :(得分:2)

您必须使用DataTable来获得所需的结果,如下所示:

Dim ds As New DataSet

Dim table1 As DataTable = New DataTable("data")
table1.Columns.Add("Column1")
table1.Columns.Add("Column2")
table1.Rows.Add("John", 49)

ds.Tables.Add(table1)

Dim myJson As String = JsonConvert.SerializeObject(ds, Formatting.Indented, New JsonSerializerSettings With {.ContractResolver = New CamelCasePropertyNamesContractResolver()})

输出

{
  "data": [
    {
      "column1": "John",
      "column2": "49"
    }
  ]
}

DataSet转换为Json时,该DataTables的{​​{1}}的名称用于生成Json数据。

答案 1 :(得分:1)

仅获取更多信息。我找到了另一种方法来做我想做的事情-请参见下面的代码。

       'Declaring the string for the JSON file
        Dim myJson As String = String.Empty

       'Checking if the dataset is empty or not
        If ds.Tables.Count > 0 Then
            ds.Tables(0).TableName = "data"
            'Converting the dataset result to JSON
            myJson = JsonConvert.SerializeObject(ds, Formatting.Indented, New JsonSerializerSettings With {
                            .ContractResolver = New CamelCasePropertyNamesContractResolver()
                            })
        End If