使用VBScript解码/编码JSON

时间:2012-08-28 06:35:07

标签: json vbscript

我有一个ERP系统的快速开发工具,它只允许使用vbscript。我正在尝试使用VBS创建一个简单的AJAX-Request。这适用于“Microsoft.XMLHTTP”对象。

下一步是使用json从Web服务器接收数据。但是在VBS中似乎没有像“json_decode”这样的函数。

有谁知道解决方案?或者是开发自己的json函数的唯一选择吗?

5 个答案:

答案 0 :(得分:9)

由于JSON是一种分层数据格式,正如彼得所提议的那样,使用正则表达式和Split()不会让你走得太远。

如果您的环境允许CreateObject(),您可以使用以另一种语言编写的现成COMponent(例如,在.WSC中包装标准json2.js或启用.NET DLL的COM)。另一种选择是通过Microsoft Script Control利用另一种语言。这种方法的结论是你必须处理由另一种语言提供的对象/数组(一些提示可以在Peter所提到的主题中找到)。

可以找到纯粹的VBScript解决方案here。我无法阅读文档,但代码编译并“适用于”简单测试用例 - YMMV。

答案 1 :(得分:8)

用ASPJSON做这个怎么样?
可从http://www.aspjson.com/

获取

我即将使用它作为一个非常古老的站点的解决方案,将带有编码数据的ajax调用(使用Jquery)发送到MongoDB进行测试。

答案 2 :(得分:3)

我有类似的问题所以我在VBScript中为我的一个项目编写了一个JSONtoXML函数。对此脚本不做任何保证(它按原样提供,并且已知有限制,例如不处理所有类型的转义序列):

Const stateRoot = 0
Const stateNameQuoted = 1
Const stateNameFinished = 2
Const stateValue = 3
Const stateValueQuoted = 4
Const stateValueQuotedEscaped = 5
Const stateValueUnquoted = 6
Const stateValueUnquotedEscaped = 7

Function JSONToXML(json)
  Dim dom, xmlElem, i, ch, state, name, value
  Set dom = CreateObject("Microsoft.XMLDOM")
  state = stateRoot
  For i = 1 to Len(json)
    ch = Mid(json, i, 1)
    Select Case state
    Case stateRoot
      Select Case ch
      Case "["
        If dom.documentElement is Nothing Then
          Set xmlElem = dom.CreateElement("ARRAY")
          Set dom.documentElement = xmlElem
        Else
          Set xmlElem = XMLCreateChild(xmlElem, "ARRAY")
        End If
      Case "{"
        If dom.documentElement is Nothing Then
          Set xmlElem = dom.CreateElement("OBJECT")
          Set dom.documentElement = xmlElem
        Else
          Set xmlElem = XMLCreateChild(xmlElem, "OBJECT")
        End If
      Case """"
        state = stateNameQuoted 
        name = ""
      Case "}"
        Set xmlElem = xmlElem.parentNode
      Case "]"
        Set xmlElem = xmlElem.parentNode
      End Select
    Case stateNameQuoted 
      Select Case ch
      Case """"
        state = stateNameFinished
      Case Else
        name = name + ch
      End Select
    Case stateNameFinished
      Select Case ch
      Case ":"
        value = ""
        State = stateValue
      End Select
    Case stateValue
      Select Case ch
      Case """"
        State = stateValueQuoted
      Case "{"
        Set xmlElem = XMLCreateChild(xmlElem, "OBJECT")
        State = stateRoot
      Case "["
        Set xmlElem = XMLCreateChild(xmlElem, "ARRAY")
        State = stateRoot
      Case " "
      Case Chr(9)
      Case vbCr
      Case vbLF
      Case Else
        value = ch
        State = stateValueUnquoted
      End Select
    Case stateValueQuoted
      Select Case ch
      Case """"
        xmlElem.setAttribute name, value
        state = stateRoot
      Case "\"
        state = stateValueQuotedEscaped
      Case Else
        value = value + ch
      End Select
    Case stateValueQuotedEscaped ' @@TODO: Handle escape sequences
      value = value + ch
      state = stateValueQuoted
    Case stateValueUnquoted
      Select Case ch
      Case "}"
        xmlElem.setAttribute name, value
        Set xmlElem = xmlElem.parentNode
        state = stateRoot
      Case "]"
        xmlElem.setAttribute name, value
        Set xmlElem = xmlElem.parentNode
        state = stateRoot
      Case ","
        xmlElem.setAttribute name, value
        state = stateRoot
      Case "\"
         state = stateValueUnquotedEscaped
      Case Else
        value = value + ch
      End Select
    Case stateValueUnquotedEscaped ' @@TODO: Handle escape sequences
      value = value + ch
      state = stateValueUnquoted
    End Select
  Next
  Set JSONToXML = dom
End Function

Function XMLCreateChild(xmlParent, tagName)
  Dim xmlChild
  If xmlParent is Nothing Then
    Set XMLCreateChild = Nothing
    Exit Function
  End If
  If xmlParent.ownerDocument is Nothing Then
    Set XMLCreateChild = Nothing
    Exit Function
  End If
  Set xmlChild = xmlParent.ownerDocument.createElement(tagName)
  xmlParent.appendChild xmlChild
  Set XMLCreateChild = xmlChild
End Function

答案 3 :(得分:0)

你最好根据json和asp上的查询推出自己的。比如这个Any good libraries for parsing JSON in Classic ASP? 大多数时候使用json2库,但这是基于jscript所以没有选择。 大多数情况下,这种JSON具有固定的结构,因此使用Regularexpression进行解析不应该像我在上面的一些答案中所证明的那样。 您可以发布一些JSON,以便我们可以使用一些例程来测试它。

答案 4 :(得分:0)

查看https://github.com/rcdmk/aspJSON

不确定这是否与Logan的回答中提到的www.ASPJSON.com(现已解散)有任何关系。

相关问题