我有下面的代码(用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",
},
答案 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