Is There a JSON Parser for VB6 / VBA?

ghz 7months ago ⋅ 86 views

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?

Answers

Consuming web services and handling modern data formats like JSON can be challenging in VB6 due to its age and limited built-in support for such technologies. However, you can still achieve this by using third-party libraries and some workaround techniques.

Handling SOAP in VB6

VB6's SOAP Toolkit has limitations, especially with complex data types. It might struggle with handling objects and nested structures. If you must use SOAP, ensure that your service returns simple data types or consider serializing complex data into strings (e.g., XML or JSON) that VB6 can handle more easily.

Using JSON in VB6

Using JSON is a viable alternative since it can be more straightforward to parse compared to complex SOAP objects. For JSON parsing in VB6, you can use third-party libraries. Here are some options:

1. VB-JSON (vbgore/vbjson)

VB-JSON is a popular JSON parser for VB6. It allows you to parse JSON strings into VB6 data structures.

  • Download VB-JSON: You can find the VB-JSON library on GitHub or various other sources online. Here’s a link to the GitHub repository: vbgore/vbjson.

  • Usage Example:

    ' Include the VB-JSON module in your project
    ' You might need to add the module (bas file) to your VB6 project
    
    Private Sub Command1_Click()
        Dim jsonString As String
        Dim jsonObject As Object
        Dim jsonParser As Object
    
        ' JSON string example
        jsonString = "{""name"": ""John Doe"", ""age"": 30, ""city"": ""New York""}"
    
        ' Create the JSON parser object
        Set jsonParser = New clsJSON
        Set jsonObject = jsonParser.parse(jsonString)
    
        ' Access JSON data
        MsgBox "Name: " & jsonObject("name")
        MsgBox "Age: " & jsonObject("age")
        MsgBox "City: " & jsonObject("city")
    End Sub
    

2. Chilkat JSON ActiveX

Chilkat provides a comprehensive set of ActiveX components, including a JSON parser.

  • Download Chilkat: You can get the JSON ActiveX component from the Chilkat website: Chilkat JSON ActiveX.

  • Usage Example:

    ' First, register the Chilkat ActiveX DLL
    ' regsvr32 ChilkatAx-9.5.0-win32.dll
    
    Private Sub Command1_Click()
        Dim json As New ChilkatJsonObject
        Dim success As Long
        Dim name As String
        Dim age As Long
        Dim city As String
    
        ' JSON string example
        Dim jsonString As String
        jsonString = "{""name"": ""John Doe"", ""age"": 30, ""city"": ""New York""}"
    
        ' Load JSON string
        success = json.Load(jsonString)
        If (success <> 1) Then
            MsgBox "Failed to load JSON."
            Exit Sub
        End If
    
        ' Access JSON data
        name = json.StringOf("name")
        age = json.IntOf("age")
        city = json.StringOf("city")
    
        MsgBox "Name: " & name
        MsgBox "Age: " & age
        MsgBox "City: " & city
    End Sub
    

Integrating with Web Services

  1. HTTP Requests: Use the MSXML2.ServerXMLHTTP object to make HTTP requests to your web service.
  2. Parsing Responses: Once you receive the JSON response, use one of the above JSON parsers to handle the data.

Example of Making an HTTP Request

Private Sub Command1_Click()
    Dim xmlhttp As Object
    Dim jsonResponse As String
    Dim jsonObject As Object
    Dim jsonParser As Object

    ' Create the XMLHTTP object
    Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")

    ' Make the HTTP request
    xmlhttp.Open "GET", "http://yourwebservice.com/api/data", False
    xmlhttp.send

    ' Get the response text
    jsonResponse = xmlhttp.responseText

    ' Parse the JSON response
    Set jsonParser = New clsJSON
    Set jsonObject = jsonParser.parse(jsonResponse)

    ' Access JSON data
    MsgBox "Name: " & jsonObject("name")
    MsgBox "Age: " & jsonObject("age")
    MsgBox "City: " & jsonObject("city")

    ' Clean up
    Set xmlhttp = Nothing
End Sub

Summary

  • SOAP: Use VB6's SOAP Toolkit for simple data types. Consider serializing complex data to strings.
  • JSON: Use third-party libraries like VB-JSON or Chilkat JSON ActiveX for parsing JSON.
  • HTTP Requests: Use MSXML2.ServerXMLHTTP for making web service requests.

By leveraging these tools and techniques, you can effectively consume web services and handle JSON data in VB6.