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/NewItemscreate child nodes,Item/Itemsadd/modify members on nodes - Default Member:
Rootis the default member, allowing direct access viaJson("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
Method 1: Using the Global Instance (Recommended)
vb
'Use the built-in global Json instance of VBMAN
With VBMAN.Json
.Item("name") = "John"
MsgBox .Encode()
End WithMethod 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 WithQuick 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 WithExample 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 WithExample 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 WithDocumentation Navigation
- Quick Start - 5-minute guide
- API Reference - Complete properties and methods
- Code Examples - Detailed code examples
Important Notes
- Type Correspondence: Dictionary corresponds to JSON object
{...}, Collection corresponds to JSON array[...] - Root is Default Member: Directly access with
Json("key"), equivalent toJson.Root("key"), used for chained read/write - NewItem/NewItems Create Nodes:
NewItemcreates child object nodes,NewItemscreates child array nodes - Item/Items Modify Members:
Itemsets key-value pairs on object nodes,Itemsadds elements on array nodes - Array Index Starts from 1: VB's Collection object indexing starts from 1, unlike JavaScript
- Global Instance Shares Data:
VBMAN.Jsonis a global instance, data persists between uses; call.Clear()when necessary