I need to create a JSON string from my data.

I need to be able to return a JSON string containing the results of an SQL query.  I know there are 3rd party tools that can simplify this, but I wanted to not have to add more to my project.

 

I built a small WinForm project to determine how to convert text into a JSON-formatted string.  Even though my source data would be from an SQL results set, for my test I just generated some data.  You only have to change the Vendor.GetVendorInfo() to accommodate whatever means you use to supply your data.

It is important to note the decorations used in the Vendor and Vendors classes.  This is needed for the JSON serialization.

The Form1 class is just a WinForm with a Start and Exit button and a text box for displaying the resulting JSON string.  I first tried with a single Vendor instance, the v1 variable.  Once that was working, I moved on to an array of Vendor types (vendors).

The GetVendorInfo() just creates the data to be serialized.  The BuildJSON() method does the actual serialization.

NOTE: Make sure you add a reference to System.Runtime.Serialization to the project!

 

 

The Form1 class:

 

Imports System.IO
Imports System.Runtime.Serialization.Json
Imports System.Text
 
Public Class Form1
 
    Private vendors As New Vendors
    Private v1 As Vendor
 
    Private Sub GetVendorInfo()
 
        Dim V As New Vendor
 
        ReDim vendors.Vendor(5)
        For i As Integer = 0 To vendors.Vendor.Length - 1
            vendors.Vendor(i) = V.GetVendorInfo()
        Next
 
        v1 = V.GetVendorInfo()
 
    End Sub
 
    Private Sub BuildJSON()
 
        Dim output As String
        Dim stream1 As MemoryStream = New MemoryStream()
        Dim ser As DataContractJsonSerializer = New DataContractJsonSerializer(GetType(Vendors))
        ser.WriteObject(stream1, vendors)
        Dim json As Byte() = stream1.ToArray()
        stream1.Close()
        output = Encoding.UTF8.GetString(json, 0, json.Length)
        txtJSON.Text = output
 
    End Sub
 
    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
 
        GetVendorInfo()
        BuildJSON()
 
    End Sub
 
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
 
        Application.Exit()
 
    End Sub
End Class

 

 

The Vendor class:

Imports System.Runtime.Serialization
 
<DataContract>
Public Class Vendors
    <DataMember>
    Public Property Vendor As Vendor()
End Class
 
<DataContract>
Public Class Vendor
 
    <DataMember>
    Public Property ID As Integer
    <DataMember>
    Public Property Name As String
    <DataMember>
    Public Property Address1 As String
    <DataMember>
    Public Property Address2 As String
    <DataMember>
    Public Property Address3 As String
    <DataMember>
    Public Property City As String
    <DataMember>
    Public Property State As String
    <DataMember>
    Public Property Zip As String
    <DataMember>
    Public Property Status As Integer
 
    Private IDs As Integer() = {5, 6, 7, 8}
    Private Names As String() = {"Acme Tool", "Smith & Sons", "Jones Dairy", "Bob's Tool Rental"}
    Private Address1s As String() = {"1234 First Ave.", "56 Main St.", "7890 Windsor Trail", "172 Woodland Park Circle"}
    Private Address2s As String() = {"Apt. 2B", "", "Suite 100", "Dept 52"}
    Private Address3s As String() = {"Night Mgr.", "", "Attn: Bob", ""}
    Private Citys As String() = {"Los Pedro", "Greenville", "Centertown", "Davis"}
    Private States As String() = {"NV", "KS", "MI", "TX"}
    Private Zips As String() = {"03678", "74289", "15388", "24464"}
    Private Statuss As Integer() = {1, 2, 3, 4}
 
    Private rnd As New Random()
    Private maxRndVal As Integer = IDs.Count
 
    Public Sub New()
 
        Randomize()
 
    End Sub
 
    Public Function GetVendorInfo() As Vendor
 
        Dim v As Vendor = New Vendor
        v.ID = IDs(rnd.Next(0, maxRndVal))
        v.Name = Names(rnd.Next(0, maxRndVal))
        v.Address1 = Address1s(rnd.Next(0, maxRndVal))
        v.Address2 = Address2s(rnd.Next(0, maxRndVal))
        v.Address3 = Address3s(rnd.Next(0, maxRndVal))
        v.City = Citys(rnd.Next(0, maxRndVal))
        v.State = States(rnd.Next(0, maxRndVal))
        v.Zip = Zips(rnd.Next(0, maxRndVal))
        v.Status = Statuss(rnd.Next(0, maxRndVal))
 
        Return v
 
    End Function
 
End Class

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences