I have the following code which works well:
Dim dept As New ArrayList
Dim forename As New ArrayList
objJSONStringBuilder = New StringBuilder()
objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))
objSQLCommand = New SqlCommand("select dept, forename from table1", objSQLConnection)
objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()
While objSQLDataReader.Read()
dept.Add(New With {Key .dept = objSQLDataReader("dept")})
forename.Add(New With {Key .forename = objSQLDataReader("forename")})
End While
objSQLDataReader.Close()
objSQLCommand.Connection.Close()
Dim serializer As New JavaScriptSerializer()
Dim arrayJson As String = serializer.Serialize(dept)
Return arrayJson
Which gives me:
[
{
"dept": "dept1"
},
{
"dept": "dept2"
},
{
"dept": "dept3"
}
]
How do I add the second column forename in the json data too so it returns:
[
{
"dept": "dept1",
"forename": "adam"
},
{
"dept": "dept2",
"forename": "joe"
},
{
"dept": "dept3",
"forename": "smith"
}
]
Eventually, I will want to add more columns, i.e. surname.
You need to make a single object with two properties: