将Dictionary脚本对象作为参数

时间:2016-02-10 19:01:04

标签: object dictionary vbscript scripting asp-classic

我宣布了一个这样的对象:

dim oTest
oTest = CreateObject("Scripting.Dictionary")
oTest.Add ("a", "Red")
oTest.Add ("b", "Blue")

我希望将此信息传递到另一个页面:

response.redirect ("nextpage.aspx?username=fred&paramstring=" & oTest)

但是我收到了这个错误:

  

运营商'&'未定义字符串“nextpage.aspx?username = fred”并键入“Dictionary”。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

(de)序列化词典的函数:

Option Explicit

Const csSD1 = "&"
Const csSD2 = "="

' make dictionary from string containing serialized dictionary (key-csSD2-value-csSD1-...)
Function s2d(s)
  Dim d : Set d = CreateObject("Scripting.Dictionary")
  Dim e, p
  For Each e In Split(s, csSD1)
      p = Split(e, csSD2)
      d(p(0)) = p(1)
  Next
  Set s2d = d
End Function

' serialize dictionary to string (key-csSD2-value-csSD1-...)
Function d2s(d)
  Dim a : a = d.Keys
  Dim i
  For i = 0 To UBound(a)
       a(i) = Join(Array(a(i), d(a(i))), csSD2)
  Next
  d2s = Join(a, csSD1)
End Function

Dim s : s = "a=red&b=blue"
Dim d : Set d = s2d(s)
If s = d2s(d) Then WScript.Echo "ok"

答案 1 :(得分:0)

Scripting.Dictionary有两种可能有用的方法。

  • .Keys() - 返回Array个键
  • .Items() - Returns an数组项目

使用这两个数组,您可以遍历它们并构建一个String。这样的事情应该有效。

<%
Dim DirTest
Dim DirKeys, DirItems
Dim Index, NumOfKeys, Params

Set DirTest = Server.CreateObject("Scripting.Dictionary")
With DirTest
  Call .Add("a", "Red")
  Call .Add("b", "Blue")
  DirKeys = .Keys()
  DirItems = .Items()
End With
If IsArray(DirKeys) Then
  Params = ""
  NumOfKeys = UBound(DirKeys)
  For Index = 0 To NumOfKeys
    Params = Params & DirKeys(Index) & "=" & DirItems(Index) & "&"
  Next
  'Trim the last Ampersand
  Params = Left(Params, Len(Params) - 1)
  Response.Write Params
End If
%>

输出:

a=Red&b=Blue