3

I'm calling a web service in VB6 which returns a json string as response. I'm able to hold the response in a string. now I want to show the each parameter separately how can I extract the values from the string ?. a sample string is here :

{
    "id": "22144",
    "t" : "AAPL",
    "e" : "NASDAQ",
    "l" : "108.00",
    "l_fix" : "108.00",
    "l_cur" : "108.00",
    "s": "2",
    "ltt":"4:00PM EDT",
    "lt" : "Aug 10, 4:00PM EDT",
    "lt_dts" : "2016-08-10T16:00:01Z",
    "c" : "-0.81",
    "c_fix" : "-0.81",
    "cp" : "-0.74",
    "cp_fix" : "-0.74",
    "ccol" : "chr",
    "pcls_fix" : "108.81",
    "el": "107.98",
    "el_fix": "107.98",
    "el_cur": "107.98",
    "elt" : "Aug 10, 5:16PM EDT",
    "ec" : "-0.02",
    "ec_fix" : "-0.02",
    "ecp" : "-0.02",
    "ecp_fix" : "-0.02",
    "eccol" : "chr",
    "div" : "0.57",
    "yld" : "2.11"
}
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Suman Kumar
  • 27
  • 1
  • 1
  • 7
  • 1
    Possible duplicate of [Is There a JSON Parser for VB6 / VBA?](http://stackoverflow.com/questions/2782076/is-there-a-json-parser-for-vb6-vba) – C-Pound Guru Apr 18 '17 at 21:08
  • Does this answer your question? [Parsing JSON in Excel VBA](https://stackoverflow.com/questions/6627652/parsing-json-in-excel-vba) – GSerg Jun 14 '23 at 16:29

2 Answers2

3

I've found VB-JSON works really well for parsing json in VB6.

You can download it from here.

VB-JSON: A Visual Basic 6 (VB6) JSON Parser Class Library

The .zip file that you download will contain a sample project and the library, which is called JSON.bas.

The main parser function is JSON.parse and you pass it the json string as parameter.

So in your project, you only need to include / add the JSON.bas file.

Sample Usage (from the sample project) :

Private Sub cmdObjToJSON_Click()

   Dim p As Object

   Dim sInputJson As String
   sInputJson = "{ width: '200', frame: false, height: 130, bodyStyle:'background-color: #ffffcc;',buttonAlign:'right', items: [{ xtype: 'form',  url: '/content.asp'},{ xtype: 'form2',  url: '/content2.asp'}] }"

   MsgBox "Input JSON string: " & sInputJson

   ' sets p
   Set p = JSON.parse(sInputJson)

   MsgBox "Parsed object output: " & JSON.toString(p)

   MsgBox "Get Bodystyle data: " & p.Item("bodyStyle")

   MsgBox "Get Form Url data: " & p.Item("items").Item(1).Item("url")


   p.Item("items").Item(1).Add "ExtraItem", "Extra Data Value"

   MsgBox "Parsed object output with added item: " & JSON.toString(p)


End Sub

As it applies to your case. Something like the following might work (with some tweaks if needed).

Dim parsedJsonObject As Object
Set parsedJsonObject = JSON.parse(yourJsonStringVariable)

'Print the ticker ( t in your json )
Debug.Print parsedJsonObject.Item("t")
SeanPatel
  • 156
  • 1
  • 1
  • 8
1

There is a JSON parser library for Visual Basic that you can find in http://json.org/. You can either use VB-JSON or PW.JSON.