Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

simple json w/MSScript control

$
0
0
simple example of using the msscript control to parse json

Code:

Public code
Public message

Private Sub Form_Load()
   
    Dim js As New CJSON
   
    Const j1 = "{""code"":1,""message"":""this is my message""}"
   
    If Not js.LoadJson(j1) Then
        Debug.Print "Failed to load json 1"
        Exit Sub
    End If
   
    Debug.Print "code exists? " & js.fieldExists("code")
    Debug.Print "code value " & js.GetVal2("code")
   
    'set multiple fields on object
    js.GetObjFields Me, "code,message"

    Debug.Print "me.code: " & Me.code
    Debug.Print "me.message: " & Me.message
   
   
'    {
'      "menu": {
'            "id": "file",
'            "value": "File",
'            "popup": {
'              "menuitem": [
'                {"value": "New"},
'                {"value": "Open"},
'                {"value": "Close"}
'              ]
'            }
'      }
'    }

    If Not js.LoadJson(Text1) Then
        Debug.Print "Failed to load text1 json"
        Exit Sub
    End If
   
    Dim sz As Long, i As Long
   
    Debug.Print "menu.popup.menuitem.length exists? " & js.fieldExists("menu.popup.menuitem.length")
   
    sz = js.GetVal2("menu.popup.menuitem.length")
    For i = 0 To sz - 1
        Debug.Print "menuitem[" & i & "] = " & js.GetVal2("menu.popup.menuitem[" & i & "].value")
    Next
   
    End
   
End Sub

Attached Files

Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>