Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse json string in vb6

Tags:

json

parsing

vb6

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"
}
like image 334
Suman Kumar Avatar asked Oct 21 '25 03:10

Suman Kumar


2 Answers

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")
like image 158
SeanPatel Avatar answered Oct 22 '25 17:10

SeanPatel


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.

like image 23
Visual Studio Guy Avatar answered Oct 22 '25 19:10

Visual Studio Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!