查看 RecordSet 并使用 Dictionary 创建 JSON 输出
Looking through RecordSet and creating JSON output with Dictionary
我正在尝试根据用户发送到 Web 服务的查询生成 JSON 输出(return 值)。
我目前使用的代码是(仅供测试):
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sqlSelect As String = "firstName, lastName, email" '3 field names to search by
Dim _count As Integer = 2 'Has 2 records returned
Dim _QueryData As New Dictionary(Of String, String)
Dim _Fields As String() = sqlSelect.Split(",")
For _int As Integer = 0 To _count - 1
For index As Integer = 0 To _Fields.Count - 1
_QueryData.Add(_Fields(index).Trim, "something" & index)
Next
Next
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(
_QueryData,
Formatting.Indented)
)
End Sub
这里有个问题我能想到:
(1) 使用字典我仅限于一个 "key" 所以一旦它再次循环第二条记录,它会给出一个 "key" 已经存在的错误。但是如果下一条记录没有相同的字段 ("key"),它就不会是 JSON 字符串...
如何修改代码以使其按我需要的方式工作?
我可以像这样手动完成:
Dim sqlSelect As String = "firstName, lastName, email" '3 field names to search by
Dim _count As Integer = 2 'Has 2 records returned
Dim _QueryData As String = "{"
Dim _Fields As String() = sqlSelect.Split(",")
For _int As Integer = 0 To _count - 1
If _int >= 1 Then _QueryData &= "," & vbCrLf & "{" & vbCrLf
For index As Integer = 0 To _Fields.Count - 1
_QueryData &= """" & _Fields(index).Trim & """" & ":" & """" & "something" & index & """," & vbCrLf
Next
_QueryData = _QueryData.Substring(0, _QueryData.Length - 3) & vbCrLf & "}"
Next
产生:
{
"firstName":"something0",
"lastName":"something1",
"email":"something2"
},
{
"firstName":"something0",
"lastName":"something1",
"email":"something2"
}
如果您正在执行查询,您应该能够将其读入数据表,然后进行简单的序列化:
Dim sJSONObject As String
Dim sqlSelect As String = "firstName,lastName,email" '3 field names to search by
Dim _count As Integer = 2 'Has 2 records returned
Dim _QueryData As New DataTable()
Dim _Fields As String() = sqlSelect.Split(",")
Dim arrData As String()
For Each sFieldName In _Fields
_QueryData.Columns.Add(sFieldName)
Next
For _int As Integer = 0 To _count - 1
ReDim arrData(_Fields.Count - 1)
For index As Integer = 0 To _Fields.Count - 1
arrData(index) = "something" & index
Next
_QueryData.Rows.Add(arrData)
Next
sJSONObject = JsonConvert.SerializeObject(
_QueryData, Formatting.Indented)
这导致:
[{
"firstName": "something0",
"lastName": "something1",
"email": "something2"
},
{
"firstName": "something0",
"lastName": "something1",
"email": "something2"
}]
如果方括号有问题,只需在发送前trim将它们去掉
我正在尝试根据用户发送到 Web 服务的查询生成 JSON 输出(return 值)。
我目前使用的代码是(仅供测试):
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sqlSelect As String = "firstName, lastName, email" '3 field names to search by
Dim _count As Integer = 2 'Has 2 records returned
Dim _QueryData As New Dictionary(Of String, String)
Dim _Fields As String() = sqlSelect.Split(",")
For _int As Integer = 0 To _count - 1
For index As Integer = 0 To _Fields.Count - 1
_QueryData.Add(_Fields(index).Trim, "something" & index)
Next
Next
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(
_QueryData,
Formatting.Indented)
)
End Sub
这里有个问题我能想到:
(1) 使用字典我仅限于一个 "key" 所以一旦它再次循环第二条记录,它会给出一个 "key" 已经存在的错误。但是如果下一条记录没有相同的字段 ("key"),它就不会是 JSON 字符串...
如何修改代码以使其按我需要的方式工作?
我可以像这样手动完成:
Dim sqlSelect As String = "firstName, lastName, email" '3 field names to search by
Dim _count As Integer = 2 'Has 2 records returned
Dim _QueryData As String = "{"
Dim _Fields As String() = sqlSelect.Split(",")
For _int As Integer = 0 To _count - 1
If _int >= 1 Then _QueryData &= "," & vbCrLf & "{" & vbCrLf
For index As Integer = 0 To _Fields.Count - 1
_QueryData &= """" & _Fields(index).Trim & """" & ":" & """" & "something" & index & """," & vbCrLf
Next
_QueryData = _QueryData.Substring(0, _QueryData.Length - 3) & vbCrLf & "}"
Next
产生:
{
"firstName":"something0",
"lastName":"something1",
"email":"something2"
},
{
"firstName":"something0",
"lastName":"something1",
"email":"something2"
}
如果您正在执行查询,您应该能够将其读入数据表,然后进行简单的序列化:
Dim sJSONObject As String
Dim sqlSelect As String = "firstName,lastName,email" '3 field names to search by
Dim _count As Integer = 2 'Has 2 records returned
Dim _QueryData As New DataTable()
Dim _Fields As String() = sqlSelect.Split(",")
Dim arrData As String()
For Each sFieldName In _Fields
_QueryData.Columns.Add(sFieldName)
Next
For _int As Integer = 0 To _count - 1
ReDim arrData(_Fields.Count - 1)
For index As Integer = 0 To _Fields.Count - 1
arrData(index) = "something" & index
Next
_QueryData.Rows.Add(arrData)
Next
sJSONObject = JsonConvert.SerializeObject(
_QueryData, Formatting.Indented)
这导致:
[{
"firstName": "something0",
"lastName": "something1",
"email": "something2"
},
{
"firstName": "something0",
"lastName": "something1",
"email": "something2"
}]
如果方括号有问题,只需在发送前trim将它们去掉