Solved

JSON Response and request in VB6 application

Posted on 2016-09-28
11
1,323 Views
Last Modified: 2016-10-12
I am working with a VB6 application. I need to send an HTTP request and the receive JSON response. I then need to deserialize the response. I have done this already in VB.NET web application.

I am wondering if it is possible in VB6 application. If yes, can you please advise? Thanks!
0
Comment
Question by:Angel02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
11 Comments
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 41821258
That should be possible. I made a pure VBA solution using collections to handle the Json response. All code is for download here:

VBA.CVRAPI

It is for a very specific purpose, probably not relevant for you, but the crucial generic parts are kept in the modules named Json. Also, study the demo module for ideas how to implement the generic tools.

/gustav
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41821279
What Kind of HTTP request? Good Web API's offer different result formats, e.g. XML.
0
 

Author Comment

by:Angel02
ID: 41822588
Thank you Gustav. Your code worked perfectly.
Now I am trying to read data from the response into a recordset as needed by my application.

Is it possible to assign data from DataCollection to  a ADODB recordset?

@ste5an
I am currently making GET requests. I later also need to make POST requests.

I used the following in Gustav's code

Set XmlHttp = CreateObject("Microsoft.XMLHTTP")
XmlHttp.Open "GET", ServiceUrl, Async
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

Expert Comment

by:Gustav Brock
ID: 41822951
Is it possible to assign data from DataCollection to  a ADODB recordset?

Certainly. You would loop the collection holding the response and, item by item, add a record to your recordset reading the fields of the collection.

/gustav
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41823085
It's not about GET or POST.

Normally you can specify your result type by using the appropriate accept header. Either accept: application/xml or accept: application/json.
0
 

Author Comment

by:Angel02
ID: 41825843
@stef5n Sorry. Get it now. Gustav's code is working good for responses. I would now like to know about sending requests.

@Gustav
Change of plan. I am now sending the jSon response to a table in SQL server. I got that to work.

I do want to know if your code has a way of sending a json request too. Below is a sample request that I need to send.

{
    "securityID": "65gfggw",
    "productId": 3452,
    "quantity": "5"
   }
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 41825884
Sorry, no. As you may have seen, the code just sends a URL with appended parameters, that's all. So there are no efforts to encode data as Json and post these.

I'm sure the Json modules could be expanded to cover this, but I just haven't had the need and, currently, I have no time for working with it, though it could be fun.

/gustav
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 41825890
Well, I located this code here which seems to do exactly what you are after:
    Dim objHTTP As Object
    Dim Json As String
    Json = Range("A15") 'here I am pulling in an existing json string to test it. String is created in other VBA code

    Dim result As String

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URl = "http://myApi/iSendJsonto/"
    objHTTP.Open "POST", URl, False

   objHTTP.setRequestHeader "Content-type", "application/json"
   objHTTP.send (Json)
   result = objHTTP.responseText

   'Some simple debugging
   Range("A25").Value = result
   Range("A26").Value = Json

   Set objHTTP = Nothing

Open in new window

It uses late binding which you could replace with early binding as in my function RetrieveDataResponse and, of course, you will have to replace the use of ranges to collect the response with variables or collections as to your need.

/gustav
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41826172
I never said, that Gustav's approach is not working or incorrect. I just added a comment, that you maybe don't need to work with XML. Cause good REST services offer multiple answer formats.
0
 

Author Comment

by:Angel02
ID: 41840207
@ste5fan I agree
@gustav thank you!
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question