lunes, 20 de abril de 2009

json parser in visual basic 6

json parser in visual basic 6

Recently, in work, I encountered with the following situation. A visual basic 6 desktop program need to comunicate with our main web application via web services. This webservices are json based. Until recently, there were no json librery / parser for visual basic 6. Today I found the only (seems) json library for visual basic.

My knowledge of vb6 is short and I don't like it (or eny other ms devel tools). But it seems that you can embed a javascript aprser in your vb6 programs using "MSScriptControl.ScriptControl.1" object. So the json parser is there! With this little class:

'a very little json library in vb using MSScriptControl.ScriptControl.1
'@author: sgurin.

Dim ScriptControl
Dim initialized As Boolean

Public Sub init_class()
Set ScriptControl = CreateObject("MSScriptControl.ScriptControl.1")
ScriptControl.Language = "JavaScript"
initialized = True
End Sub

Private Function evalCode(json As String) As String
json = Replace(json, """", "\""")
evalCode = "eval("" " & json & " "") "
End Function

Public Function getProperty(jsonObject As String, prop As String) As String
Dim ret As String
Dim jsCode As String
If Not initialized Then
init_class
End If
jsCode = "var obj = " & jsonObject 'evalCode(jsonObject)
ScriptControl.ExecuteStatement (jsCode)
getProperty = ScriptControl.Eval("obj[""" & prop & """]")
End Function

'for example getSimpleArrayItem("[{foo:1}, {foo:2}]", 0, "foo") == 1
Public Function getSimpleArrayItem(jsonArray As String, index As Integer, prop As String) As String
Dim jsCode As String
If Not initialized Then
init_class
End If
jsCode = "var arr = " & jsonArray
ScriptControl.ExecuteStatement (jsCode)
getSimpleArrayItem = ScriptControl.Eval("arr[" & index & "][""" & prop & """]")
End Function

Public Function getArrayLenght(jsonArray As String) As Integer
Dim jsCode As String
If Not initialized Then
init_class
End If
jsCode = "var arr = " & jsonArray
ScriptControl.ExecuteStatement (jsCode)
getArrayLenght = ScriptControl.Eval("function js_arrayLength(a){if(a==null||a.length==null||a.length==undefined){return 0}else return a.length;}js_arrayLength(arr)")
End Function




using this class one can parse a json string with for example:

Dim parser As New jsonParser
Dim s as String
s="{age:18}"
msgbox "age: " & parser.getProperty(s, "age")


of another example:

Dim parser As New jsonParser
Dim size As Integer, i As Integer, s As String, json as String
json="[{age:18},{age:22}]"
s=" ages: "
size = parser.getArrayLenght(resp)
For i = 0 To size - 1
s = s & parser.getSimpleArrayItem(resp, i, "age") & ", "
Next i

Well, I hope somebody found this useful. Please feel free to use this code as a fundation for a real json parser in visual basic 6. We need more !

2 comentarios:

Garfinkel dijo...

This is a JSON parser I use a lot from within my server side VB6 based website rendering DLL. It could be used with a HTTP get function to interact with JSON based web services:

VB6 JSON Web Services Parser ClassRegards,
Mike

Cancerbero dijo...

Hi mike: thanks! about a year ago I make an extensive search looking for some vb6 json parser and I didn't found nothing. In www.json.org there where nothing until a month (the same project pointed by you). I think this is the only json parser written in vb6 around... Nevertheless the alternative of using the ms javascript parser still can be valid in some situations... If I was a big fan of vb perhaps I would enrich a little more this alternative, but it is not the case :(