我有一个用于ERP系统的快速开发工具,只支持vbscript语言。我正在尝试使用VBS创建一个简单的AJAX请求。使用“Microsoft.XMLHTTP”对象可以实现这一点。
下一步是使用json从Web服务器接收数据。但在VBS中似乎没有像“json_decode”或其他的函数。
是否有人知道解决方案?或者唯一的选择是开发自己的json函数?
由于JSON是一种分层数据格式,因此采用Peter提出的正则表达式和Split()方法并不能帮助你解决问题。
如果您的环境允许使用 CreateObject()
方法,则可以使用另一种语言编写的现成COM组件(例如将标准的json2.js包装在.WSC文件中或启用.NET DLL的COM)。另一种选择是通过Microsoft Script Control来利用另一种语言。这种方法的缺点是您必须处理由其他语言交付的对象/数组(有关提示请参见Peter所提到的主题)。
可以在这里找到纯VBScript解决方案。我无法阅读文档,但代码能够编译并对简单测试用例有效 - YMMV。
如何使用ASPJSON完成此操作?
可从http://www.aspjson.com/获取。
我将把这个作为解决方案应用于一个非常老的网站,通过发送编码数据的ajax调用(使用Jquery)到MongoDB进行测试。
我曾经遇到过类似的问题,所以我为我的一个项目编写了一个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