如何在.VBS脚本中解码JSON

时间:2016-10-10 23:52:11

标签: json vbscript

我尝试通过MSXML2.ServerXMLHTTP上的get请求接收JSON,然后将其解析为VBS字典。

我能够以恰当的JSON格式接收响应。但是,当我在我的请求的responseText上使用this VbsJson class's decode method时,我遇到了错误。

  

无法将变体类型(Dispatch)转换为Olestr

我是vbs的绝对新手,想要一种简单的方法将这些数据放入字典中(如果我还可以定义我想要提取的字段,那就更好了。)

代码:

Sub Search(Sender)
  Dim Resp, Data
  Dim UrlToGet2
  UrlToGet2 = http://www.mywebsite.com/json
  Set Data = CreateObject("Scripting.Dictionary")
  Dim xHttp: Set xHttp = createobject("MSXML2.ServerXMLHTTP")

  xHttp.Open "GET", UrlToGet2, False
  xHttp.setRequestHeader "Content-Type", "application/json"

  xHttp.Send

  If xHttp.Status <> 200 Then
    Set Resp = CreateObject("Scripting.Dictionary")
    Resp.Add "success", "false"
    Resp.Add "error", "HTTP Error: " & xHttp.Status & " " & xHttp.StatusText
    Exit Sub
  End If

  Set Resp = CreateObject("Scripting.Dictionary")
  Dim x, json
  Set json = New VbsJson
  Set x = json.Decode(xHttp.responseText)
  With Resp
    Dim a: a = x.Keys
    Dim i
    For i = 0 To x.Count -1
      .Add a(i), x(a(i))
    Next
  End With
  Set xHttp = Nothing
End Sub

0 个答案:

没有答案