【问题标题】:Looking through RecordSet and creating JSON output with Dictionary查看 RecordSet 并使用 Dictionary 创建 JSON 输出
【发布时间】:2015-04-30 16:28:08
【问题描述】:

我正在尝试从用户发送到 Web 服务的查询中生成 JSON 输出(返回值)。

我目前用于执行此操作的代码是(仅用于测试):

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”),它就不会是 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"
}

【问题讨论】:

    标签: json vb.net for-loop serialization dictionary


    【解决方案1】:

    如果您正在执行查询,您应该能够将其读取到数据表中,这样就可以进行简单的序列化:

        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"
    }]
    

    如果方括号有问题,只需在发送前将其剪掉

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-17
      • 2018-10-03
      • 2018-01-13
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多