PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[VB6/VBA] JSON parsing to built-in VBA.Collections-VBForums
Results 1 to 2 of 2

Thread: [VB6/VBA] JSON parsing to built-in VBA.Collections

  1. #1

    Thread Starter
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,807

    [VB6/VBA] JSON parsing to built-in VBA.Collections

    These are x64 and 32-bit implementations of JSON parsing/dumping functions that are using instances of built-in VBA.Collection to represent both composite JSON objects and arrays. The module allows to switch to alternative internal representation using Scripting.Dictionary for the JSON objects/arrays although these usually require more memory and are slower for JSONs above certain size.

    To remain agnostic to this dual internal representation the module implements an accessor property JsonItem for getting and setting JSON values (e.g. JsonItem(oJson, "path/to/key") = 42) and JsonKeys to enumerate JSON object's keys (this works for arrays too). JsonItem can be used with "wildcard" keys like this vArray = JsonItem(oJson, "receiver/phones/*/number") to return array of numbers from all entries in the phones JSON array.

    Another set of helper functions are JsonTo/FromXmlDocument which as the names suggest can be used to transcode to/from XML (e.g. when accessing SOAP services).

    thinBasic Code:
    1. Option Explicit
    2. DefObj A-Z
    3. Private Const MODULE_NAME As String = "mdJson"
    4.  
    5. #Const ImplScripting = JSON_USE_SCRIPTING <> 0
    6. #Const ImplUseShared = DebugMode <> 0
    7.  
    8. #Const HasPtrSafe = (VBA7 <> 0)
    9. #Const LargeAddressAware = (Win64 = 0 And VBA7 = 0 And VBA6 = 0 And VBA5 = 0)
    10.  
    11. ' See gist in link above
    cheers,
    </wqw>

  2. #2

    Thread Starter
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,807

    Re: [VB6/VBA] JSON parsing to built-in VBA.Collections

    Will post some sample code here

    1. Using JsonItem and JsonKeys
    thinBasic Code:
    1. Option Explicit
    2.  
    3. Sub Main()
    4.     '--- 1. Never explicitly set oJson references to New instances when
    5.     '---    `JsonItem` can create JSON objects for you
    6.    
    7.         Dim oJson As Object
    8.         JsonItem(oJson, "path/to/prop") = 42
    9.         Debug.Print JsonDump(oJson)
    10.         ' -> { "path": { "to": { "prop": 42 } } }
    11.    
    12.     '--- 2. Simple way to get an empty JSON object w/ no properties
    13.    
    14.         Dim oEmpty As Object
    15.         JsonItem(oEmpty, vbNullString) = Empty
    16.         Debug.Print JsonDump(oEmpty)
    17.         ' -> { }
    18.  
    19.     '--- 3. Simple way to get an empty JSON array
    20.    
    21.         Set oEmpty = Nothing
    22.         JsonItem(oEmpty, -1) = Empty
    23.         Debug.Print JsonDump(oEmpty)
    24.         ' -> [ ]
    25.    
    26.     '--- 4. Easily append items to a JSON array by assign to -1 index
    27.    
    28.         JsonItem(oJson, "path/to/array/-1") = 5
    29.         JsonItem(oJson, "path/to/array/-1") = 10
    30.         JsonItem(oJson, "path/to/array/-1") = 42
    31.         Debug.Print JsonDump(JsonItem(oJson, "path/to/array"))
    32.         ' -> [ 5, 10, 42 ]
    33.    
    34.     '--- 5. Convert JSON array to VB6 array by using * index
    35.    
    36.         JsonItem(oJson, "path/to/array/-1") = 2
    37.         JsonItem(oJson, "path/to/array/-1") = 3
    38.         Debug.Print Join(JsonItem(oJson, "path/to/array/*"), ", ")
    39.         ' -> 5, 10, 42, 2, 3
    40.    
    41.     '--- 6. Create JSON array of JSON objects and assign `number` property in one statement
    42.    
    43.         JsonItem(oJson, "path/to/array/*/number") = Array(1, 2, 3, 4, 5)
    44.         Debug.Print JsonDump(JsonItem(oJson, "path/to/array"))
    45.         ' -> [ { "number": 1 }, { "number": 2 }, { "number": 3 }, { "number": 4 }, { "number": 5 } ]
    46.    
    47.         Debug.Print JsonDump(oJson)
    48.         ' -> {
    49.         '        "path": {
    50.         '            "to": {
    51.         '                "prop": 42,
    52.         '                "array": [ { "number": 1 }, { "number": 2 }, { "number": 3 }, { "number": 4 }, { "number": 5 } ]
    53.         '            }
    54.         '        }
    55.         '    }
    56.        
    57.     '--- 7. Get JSON object keys as a VB6 array (works for JSON arrays too)
    58.    
    59.         JsonItem(oJson, "path/to/test") = "Now is " & Now
    60.         Debug.Print Join(JsonKeys(oJson, "path/to"), ", ")
    61.         ' -> prop, array, test
    62.    
    63.     '--- 8. Remove an item by assigning `Empty`
    64.        
    65.         JsonItem(oJson, "path/to/test") = Empty
    66.         Debug.Print Join(JsonKeys(oJson, "path/to"), ", ")
    67.         ' -> prop, array
    68.    
    69.     '--- 9. Test item for existence w/ `IsEmpty`
    70.        
    71.         Debug.Print IsEmpty(JsonItem(oJson, "path/to/nothing"))
    72.         ' -> True
    73.    
    74.     '--- 10. Reference an item as a separate JSON object and dump its keys
    75.        
    76.         Dim oItem As Object
    77.         Set oItem = JsonItem(oJson, "path/to")
    78.         Debug.Print Join(JsonKeys(oItem), ", ")
    79.         ' -> prop, array
    80.        
    81.     '--- 11. Get JSON array elements count
    82.        
    83.         Debug.Print JsonItem(oJson, "path/to/array/-1")
    84.         ' -> 5
    85.        
    86.     '--- 12. Get JSON array elements count alternative
    87.    
    88.         Debug.Print UBound(JsonKeys(oJson, "path/to/array")) + 1
    89.         ' -> 5
    90.        
    91.     '--- 13. Enumerate JSON array elements
    92.    
    93.         Dim vElem As Variant
    94.         For Each vElem In JsonKeys(oJson, "path/to/array")
    95.             Debug.Print "[" & vElem & "]: " & JsonItem(oJson, "path/to/array/" & vElem & "/number") & ", ";
    96.         Next
    97.         ' -> [0]: 1, [1]: 2, [2]: 3, [3]: 4, [4]: 5,
    98. End Sub
    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width