很容易建议您在服务器端使用 JScript 而不是 VBScript。它不仅更自然地做这类事情,而且您可能熟悉该语言。缺点是 Web 上与 ASP 相关的绝大多数“How-To”都是用 VBScript 编写的。
VBScript 中的关联数组称为库Dictionary
中可用的Scripting
。但是,要创建层次结构,您可能需要更多帮助。我会围绕着创建一个类,Dictionary
这样我就可以拥有更多的Name
属性,并使分层操作更容易。
这是一个示例类:-
Class Node
Private myName
Private myChildren
Private Sub Class_Initialize()
Set myChildren = CreateObject("Scripting.Dictionary")
End Sub
Public Property Get Name()
Name = myName
End Property
Public Property Let Name(value)
myName = Value
End Property
Public Function AddChild(value)
If Not IsObject(value) Then
Set AddChild = new Node
AddChild.Name = value
Else
Set AddChild = value
End If
myChildren.Add AddChild.Name, AddChild
End Function
Public Default Property Get Child(name)
Set Child = ObjectOrNothing(myChildren.Item(name))
End Property
Public Property Get Children()
Set Children = myChildren
End Property
Private Function ObjectOrNothing(value)
If IsObject(value) Then
Set ObjectOrNothing = value
Else
Set ObjectOrNothing = Nothing
End If
End Function
End Class
现在你可以创建你的树了:-
Dim root : Set root = new Node
With root.AddChild("United States")
With .AddChild("Washington")
With .AddChild("Electric City")
.AddChild "Banks Lake"
End With
With .AddChild("Lake Chelan")
.AddChild "Wapato Point"
End With
.AddChild "Gig Harbour"
End With
End With
现在访问此层次结构:-
Sub WriteChildrenToResponse(node)
For Each key In node.Children
Response.Write "<div class=""node"">" & vbCrLf
Response.Write "<div>" & root.Child(key).Name "</div>" & vbCrlF
Response.Write "<div class=""children"">" & vbCrLf
WriteChildrenToResponse root.Child(key)
Response.Write "</div></div>"
Next
End Sub
''# Dump content of root heirarchy to the response
WriteChildrenToResponse root