Write Collection in VB.net

Dec 14, 2009 at 7:58 PM

Hi James,

Thanks for providing JSON.net.  I am trying to write JSON.net collection Array but not quite getting the result.  I am using Newtonsoft.Json library  Any help or direction is much appreciated.

Expected Output:

{ "message_array": [ { "messageid": "1", "message": "message 1", "mt": "1", "location": "http://maps.google.com/maps?daddr=29.77+-95.77", "tel": "(800) 900-1234", "msgcount": "2", "sender_alias": "Push.One" } ,{ "messageid": "2", "message": "message 2", "mt": "2", "location": "http://maps.google.com/maps?daddr=29.77+-95.77", "tel": "(800) 900-1234", "msgcount": "2", "sender_alias": "Push.To" },{ "messageid": "3", "message": "message 3", "mt": "3", "location": "http://maps.google.com/maps?daddr=29.77+-95.77", "tel": "(800) 900-1234", "msgcount": "2", "sender_alias": "Push.Three" }] }

Actual Output:

{ "message_array": [ { "messageid": "1", "message": "message 1", "mt": "1", "location": "http://maps.google.com/maps?daddr=29.77+-95.77", "tel": "(800) 900-1234", "msgcount": "2", "sender_alias": "Push.One" } ] }{ "messageid": "2", "message": "message 2", "mt": "2", "location": "http://maps.google.com/maps?daddr=29.77+-95.77", "tel": "(800) 900-1234", "msgcount": "2", "sender_alias": "Push.To" }{ "messageid": "3", "message": "message 3", "mt": "3", "location": "http://maps.google.com/maps?daddr=29.77+-95.77", "tel": "(800) 900-1234", "msgcount": "2", "sender_alias": "Push.Three" }

Code below:


        WriteJSONOutPut(True, False, "1", "message 1", "1", "http://maps.google.com/maps?daddr=29.77+-95.77", "(800) 900-1234", "2", "Push.One")
        WriteJSONOutPut(False, False, "2", "message 2", "2", "http://maps.google.com/maps?daddr=29.77+-95.77", "(800) 900-1234", "2", "Push.To")
        WriteJSONOutPut(False, True, "3", "message 3", "3", "http://maps.google.com/maps?daddr=29.77+-95.77", "(800) 900-1234", "2", "Push.Three")

 

Private Sub WriteJSONOutPut(ByVal isStart As Boolean, ByVal isEnd As Boolean, ByVal MessageId As String, ByVal Message As String, _
                                ByVal MessageThreadId As String, ByVal Location As String, ByVal Tel As String, ByVal MsgCount As String, _
                                 ByVal SenderAlias As String)
        Dim sb As New StringBuilder()
        Dim sw As New StringWriter(sb)

        Using jsonWriter As JsonWriter = New JsonTextWriter(sw)
            jsonWriter.Formatting = Formatting.Indented
            If isStart Then
                jsonWriter.WriteStartObject()
                jsonWriter.WritePropertyName("message_array")

                jsonWriter.WriteStartArray()
            End If

            jsonWriter.WriteStartObject()

            jsonWriter.WritePropertyName("messageid")
            jsonWriter.WriteValue(MessageId)
            jsonWriter.WritePropertyName("message")
            jsonWriter.WriteValue(Message)

            jsonWriter.WritePropertyName("mt")
            jsonWriter.WriteValue(MessageThreadId)
            jsonWriter.WritePropertyName("location")
            jsonWriter.WriteValue(Location)
            jsonWriter.WritePropertyName("tel")
            jsonWriter.WriteValue(Tel)
            jsonWriter.WritePropertyName("msgcount")
            jsonWriter.WriteValue(MsgCount)
            jsonWriter.WritePropertyName("sender_alias")
            jsonWriter.WriteValue(SenderAlias)
         
            If isEnd Then
             

                jsonWriter.WriteEndObject()
            End If


        End Using

        Response.Write(sb.ToString)

    End Sub