如何在JSON文件中编写多个记录

  • 本文关键字:记录 JSON 文件 json.net
  • 更新时间 :
  • 英文 :


我找不到任何地方。我正在使用牛顿JSON控件,并尝试循环浏览我的记录,并将每个记录写入一个JSON文件中。以下是我正在尝试的东西,但不能正确。我遇到了令牌路径错误和各种各样的。我不知道在哪里写JSON的开始和结束。

谢谢!沃伦

        Dim sb As New StringBuilder()
    Dim sw As New StringWriter(sb)
    Using writer As JsonWriter = New JsonTextWriter(sw)
        writer.Formatting = Formatting.Indented
        writer.WriteStartObject()
        Dim ConnString7 As String = "removed"
        Dim SQLConn As New SqlConnection()
        Dim SQLCmd As New SqlCommand()
        SQLCmd.Connection = SQLConn
        SQLConn.ConnectionString = ConnString7
        SQLCmd.CommandType = CommandType.StoredProcedure
        SQLCmd.CommandText = "Sproc_Here"
        SQLConn.Open()
        Dim reader As SqlDataReader
        reader = SQLCmd.ExecuteReader()
        While reader.Read()
            MsgBox("Start")
            writer.WritePropertyName("CardDate")
            writer.WriteValue(reader("CardDate").ToString())
            writer.WritePropertyName("EditDate")
            writer.WriteValue(reader("EditDate").ToString())
            writer.WritePropertyName("Activity")
            writer.WriteValue(reader("Activity").ToString())
            writer.WritePropertyName("Location")
            writer.WriteValue(reader("Location").ToString())
            MsgBox("End")
        End While
        reader.Close()
        SQLConn.Close()
        writer.WriteEnd()
        writer.WriteEndObject()
        MsgBox(sb.ToString)
        Response.Clear()
        Response.ContentType = "application/json; charset=utf-8"
        Response.Write(sb.ToString)
        Response.End()

每个记录都将成为一个JSON对象,并且您想拥有其中的多个记录,因此需要将它们封闭在JSON数组中。这样的东西:

    Dim sb As New StringBuilder()
    Dim sw As New StringWriter(sb)
    Using writer As JsonWriter = New JsonTextWriter(sw)
        writer.Formatting = Formatting.Indented
        ' ... code to set up SQLCmd omitted for brevity ...
        Dim reader As SqlDataReader = SQLCmd.ExecuteReader()
        writer.WriteStartArray()
        While reader.Read()
            writer.WriteStartObject()
            writer.WritePropertyName("CardDate")
            writer.WriteValue(reader("CardDate").ToString())
            writer.WritePropertyName("EditDate")
            writer.WriteValue(reader("EditDate").ToString())
            writer.WritePropertyName("Activity")
            writer.WriteValue(reader("Activity").ToString())
            writer.WritePropertyName("Location")
            writer.WriteValue(reader("Location").ToString())
            writer.WriteEndObject()
        End While
        writer.WriteEndArray()
        ' ... code to close connection and write response omitted ...
    End Using

相关内容

  • 没有找到相关文章

最新更新