将数组序列化为Json

时间:2011-06-23 13:57:55

标签: asp.net vb.net json serialization

我有以下代码将数组序列化为json:

Dim col1 As New ArrayList
Dim col2 As New ArrayList

objJSONStringBuilder = New StringBuilder()
objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))

objSQLCommand = New SqlCommand("select col1, col2 from table1", objSQLConnection)

objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()

While objSQLDataReader.Read()
    col1.Add(objSQLDataReader("col1"))
End While

objSQLDataReader.Close()
objSQLCommand.Connection.Close()

Dim serializer As New JavaScriptSerializer()
Dim arrayJson As String = serializer.Serialize(col1)

Return arrayJson

返回

[
    "dept1",
    "dept2",
    "dept3",
    "dept4",
    "dept5",
    "dept6"
]

如何让它返回呢?

它自己的第二个数组col2将返回:

[
    {"department_name":"dept1"},
    {"department_name":"dept2"},
    {"department_name":"dept3"},
    {"department_name":"dept4"},
    {"department_name":"dept5"},
    {"department_name":"dept6"}
]

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

col1.Add(new { department_name = objSQLDataReader("col1")});