我正在尝试使用VB6中的web服务。由我控制的服务目前可以返回SOAP/XML消息或JSON。我真的很难弄清楚VB6的SOAP类型(版本1)是否可以处理返回的object -而不是像string、int等简单类型。到目前为止,我不知道需要做些什么才能让VB6处理返回的对象。
因此,我想我可以将web服务中的响应序列化为JSON字符串。是否存在针对VB6的JSON解析器?
发布于 2010-05-06 23:41:37
查看JSON.org,获得许多不同语言的JSON解析器的最新列表(见主页底部)。在撰写本文时,您将在那里看到几个不同的JSON解析器的链接,但只有一个是用于VB6/VBA的(其他的是.NET):
Dim p As Object
- When I tried to download the zip file, Windows said the data was corrupt. However, I was able to use [7-zip](http://www.7-zip.org/) to pull the files out. It turns out that the main "folder" in the zip file isn't recognized as a folder by Windows, by 7-zip can see the contents of that main "folder," so you can open that up and then extract the files accordingly.
- The actual syntax for this VB JSON library is really simple:p= JSON.parse(strFormattedJSON)‘打印嵌套属性的文本’Debug.Print Candidate‘打印数组内属性的文本’Debug.Print p.Item(“Candidate”)(4).Item(“ZipCode”)
注:我必须通过ActiveX编辑器中的工具>引用添加“微软脚本运行时”和“微软VBA数据对象2.8”库作为引用。
注: VBJSON代码实际上是基于谷歌代码项目的vba-json。然而,VBJSON承诺对原始版本进行几个bug修复。
发布于 2014-02-10 04:58:13
构建在ozmike解决方案上,这对我不起作用(Excel2013和IE10)。原因是我无法调用公开的JSON对象上的方法。因此,它的方法现在通过附加到DOMElement的函数公开。我不知道这是可能的(一定是那个IDispatch-东西),谢谢你ozmike。
正如ozmike所说,没有第三方库,只有30行代码。
Option Explicit
Public JSON As Object
Private ie As Object
Public Sub initJson()
Dim html As String
html = "<!DOCTYPE html><head><script>" & _
"Object.prototype.getItem=function( key ) { return this[key] }; " & _
"Object.prototype.setItem=function( key, value ) { this[key]=value }; " & _
"Object.prototype.getKeys=function( dummy ) { keys=[]; for (var key in this) if (typeof(this[key]) !== 'function') keys.push(key); return keys; }; " & _
"window.onload = function() { " & _
"document.body.parse = function(json) { return JSON.parse(json); }; " & _
"document.body.stringify = function(obj, space) { return JSON.stringify(obj, null, space); }" & _
"}" & _
"</script></head><html><body id='JSONElem'></body></html>"
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate "about:blank"
Do While .Busy: DoEvents: Loop
Do While .readyState <> 4: DoEvents: Loop
.Visible = False
.document.Write html
.document.Close
End With
' This is the body element, we call it JSON:)
Set JSON = ie.document.getElementById("JSONElem")
End Sub
Public Function closeJSON()
ie.Quit
End Function下面的测试从头开始构造一个JavaScript对象,然后将其字符串化。然后,它解析回对象并迭代它的键。
Sub testJson()
Call initJson
Dim jsObj As Object
Dim jsArray As Object
Debug.Print "Construction JS object ..."
Set jsObj = JSON.Parse("{}")
Call jsObj.setItem("a", 1)
Set jsArray = JSON.Parse("[]")
Call jsArray.setItem(0, 13)
Call jsArray.setItem(1, Math.Sqr(2))
Call jsArray.setItem(2, 15)
Call jsObj.setItem("b", jsArray)
Debug.Print "Object: " & JSON.stringify(jsObj, 4)
Debug.Print "Parsing JS object ..."
Set jsObj = JSON.Parse("{""a"":1,""b"":[13,1.4142135623730951,15]}")
Debug.Print "a: " & jsObj.getItem("a")
Set jsArray = jsObj.getItem("b")
Debug.Print "Length of b: " & jsArray.getItem("length")
Debug.Print "Second element of b: "; jsArray.getItem(1)
Debug.Print "Iterate over all keys ..."
Dim keys As Object
Set keys = jsObj.getKeys("all")
Dim i As Integer
For i = 0 To keys.getItem("length") - 1
Debug.Print keys.getItem(i) & ": " & jsObj.getItem(keys.getItem(i))
Next i
Call closeJSON
End Sub输出
Construction JS object ...
Object: {
"a": 1,
"b": [
13,
1.4142135623730951,
15
]
}
Parsing JS object ...
a: 1
Length of b: 3
Second element of b: 1,4142135623731
Iterate over all keys ...
a: 1
b: 13,1.4142135623730951,15发布于 2012-06-15 01:19:53
希望这对那些在搜索"vba json“之后不断访问这个页面的人有很大的帮助。
我发现这个page非常有帮助。它提供了几个与Excel兼容的VBA类,用于处理JSON格式的数据。
https://stackoverflow.com/questions/2782076
复制相似问题