2011-06-23 72 views
2

我有下面的代碼序列化數組JSON:數組序列化到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"} 
] 

回答

2

嘗試這樣的事:

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