Skip to content

cJson Developer Documentation

Overview

cJson is the JSON processing class provided by the VBMAN framework, improved and encapsulated based on VBA-JSON v2.3.1 (Tim Hall). It provides a simple and easy-to-use API to parse and generate JSON data, supporting nested objects, arrays, file operations, and more.

Key Features

  • Clear Type Mapping: Dictionary ↔ JSON object {...}, Collection ↔ JSON array [...]
  • Separated Node Creation and Member Setting: NewItem/NewItems create child nodes, Item/Items add/modify members on nodes
  • Default Member: Root is the default member, allowing direct access via Json("key") and supporting chained nesting
  • Nested Support: Unlimited levels of JSON object and array nesting
  • File Operations: Load and save JSON data from/to files
  • Encoding/Decoding: Bidirectional conversion between JSON strings and VB objects
  • Formatted Output: Custom indent formatting for JSON strings
  • Chinese Support: Display Chinese characters directly (no Unicode transcoding)
  • Array Indexing: Collection array indices start from 1 (following VB conventions)

Creating Instances

vb
'Use the built-in global Json instance of VBMAN
With VBMAN.Json
    .Item("name") = "John"
    MsgBox .Encode()
End With

Method 2: Creating a New Instance

vb
'Create an independent instance
Dim Json As New VBMANLIB.cJson
Json.Item("name") = "John"
MsgBox Json.Encode()

'Or use With New
With New VBMANLIB.cJson
    .Item("name") = "John"
    MsgBox .Encode()
End With

Quick Examples

Example 1: Creating a Simple JSON Object

vb
With New cJson
    .Item("code") = 200
    .Item("msg") = "Success"
    .Item("data") = "Response data"
    MsgBox .Encode(, 2, True)
End With
'Output:
'{
'  "code": 200,
'  "msg": "Success",
'  "data": "Response data"
'}

Example 2: Creating JSON with an Array

vb
With New cJson
    .Item("code") = 200
    .Item("msg") = "Success"
    With .NewItems("data")  'Create array
        With .NewItem()      'First object in array
            .Item("name") = "John"
            .Item("age") = 25
        End With
        With .NewItem()      'Second object in array
            .Item("name") = "Jane"
            .Item("age") = 30
        End With
    End With
    MsgBox .Encode(, 2, True)
End With

Example 3: Parsing a JSON String (Using Root)

vb
Dim JsonText As String
JsonText = "{""name"":""John"",""age"":25}"

With New cJson
    .Decode JsonText
    'Access using Root (default member) - recommended approach
    MsgBox .Root("name")  'Output: John
    MsgBox .Root("age")   'Output: 25

    'Or use the default member feature directly
    MsgBox $("name")      'Output: John
End With

Example 4: Convert Database Recordset to JSON in One Line

vb
'cJson.Decode directly supports ADODB.Recordset, one-liner conversion
MsgBox VBMAN.Json.Decode(Rs).Encode(, 2, True)

'Or use an independent instance
With New cJson
    .Decode VBMAN.Db.Rs
    MsgBox .Encode(, 2, True)
End With

Documentation Navigation

Important Notes

  1. Type Correspondence: Dictionary corresponds to JSON object {...}, Collection corresponds to JSON array [...]
  2. Root is Default Member: Directly access with Json("key"), equivalent to Json.Root("key"), used for chained read/write
  3. NewItem/NewItems Create Nodes: NewItem creates child object nodes, NewItems creates child array nodes
  4. Item/Items Modify Members: Item sets key-value pairs on object nodes, Items adds elements on array nodes
  5. Array Index Starts from 1: VB's Collection object indexing starts from 1, unlike JavaScript
  6. Global Instance Shares Data: VBMAN.Json is a global instance, data persists between uses; call .Clear() when necessary

VB6 and LOGO copyright of Microsoft Corporation