I am trying to consume a web service in VB6. The service - which I control - currently can return a SOAP/XML message or JSON. I am having a really difficult time figuring out if VB6's SOAP type (version 1) can handle a returned object
- as opposed to simple types like string
, int
, etc. So far I cannot figure out what I need to do to get VB6 to play with returned objects.
So I thought I might serialize the response in the web service as a JSON string. Does a JSON parser exist for VB6?
You can use free or open-source VBA libraries like VB-JSON or VBA-JSON.
It's the most popular JSON parser, according to our findings on Github usages. Oracle's JSONP: https://jsonp.java.netJSONP (JSON Processing) is a Java API for JSON processing, namely around consuming and producing streaming JSON text. It's the open source reference implementation of JSR353.
JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON.
Check out JSON.org for an up-to-date list (see bottom of main page) of JSON parsers in many different languages. As of the time of this writing, you'll see a link to several different JSON parsers there, but only one is for VB6/VBA (the others are .NET):
VB-JSON
The actual syntax for this VB JSON library is really simple:
Dim p As Object Set p = JSON.parse(strFormattedJSON) 'Print the text of a nested property ' Debug.Print p.Item("AddressClassification").Item("Description") 'Print the text of a property within an array ' Debug.Print p.Item("Candidates")(4).Item("ZipCode")
Building on ozmike solution, which did not work for me (Excel 2013 and IE10). The reason is that I could not call the methods on the exposed JSON object. So its methods are now exposed through functions attached to a DOMElement. Didn't know this is possible (must be that IDispatch-thing), thank you ozmike.
As ozmike stated, no 3rd-party libs, just 30 lines of code.
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
The following test constructs a JavaScript Object from scratch, then stringifies it. Then it parses the object back and iterates over its keys.
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
outputs
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With