cJson Quick Start Guide
1. Add Reference
Ensure your project has a reference to the VBMAN library added:
vb
'Reference in code using the following methods
Dim Json As New VBMANLIB.cJson
'Or directly use the VBMAN global instance
With VBMAN.Json
'...
End With2. Creating JSON Objects
2.1 Creating Simple Key-Value Pair Objects
vb
Private Sub CreateSimpleJson()
With New VBMANLIB.cJson
'Directly assign to create key-value pairs
.Item("servicesn") = "0001"
.Item("userid") = "admin"
.Item("token") = ""
.Item("argcounts") = 2
'Encode to JSON string
Dim JsonString As String
JsonString = .Encode(, 2, True)
Debug.Print JsonString
End With
End SubOutput:
json
{
"servicesn": "0001",
"userid": "admin",
"token": "",
"argcounts": 2
}2.2 Creating Nested Objects
vb
Private Sub CreateNestedJson()
With New VBMANLIB.cJson
.Item("name") = "John"
.Item("age") = 25
'Create nested object (using NewItem)
With .NewItem("address")
.Item("city") = "Beijing"
.Item("district") = "Haidian"
End With
Debug.Print .Encode(, 2, True)
End With
End SubOutput:
json
{
"name": "John",
"age": 25,
"address": {
"city": "Beijing",
"district": "Haidian"
}
}2.3 Creating Arrays
vb
Private Sub CreateArrayJson()
With New VBMANLIB.cJson
.Item("code") = 200
.Item("msg") = "Success"
'Create array (using NewItems)
With .NewItems("data")
'Add array elements (using NewItem to create object elements)
With .NewItem()
.Item("id") = 1
.Item("name") = "Project 1"
End With
With .NewItem()
.Item("id") = 2
.Item("name") = "Project 2"
End With
End With
Debug.Print .Encode(, 2, True)
End With
End SubOutput:
json
{
"code": 200,
"msg": "Success",
"data": [
{ "id": 1, "name": "Project 1" },
{ "id": 2, "name": "Project 2" }
]
}3. Parsing JSON Strings
3.1 Parsing Simple Objects (Using Root)
vb
Private Sub ParseSimpleJson()
Dim JsonText As String
JsonText = "{""name"":""John"",""age"":25,""city"":""Beijing""}"
With New VBMANLIB.cJson
.Decode JsonText
'Access using Root - recommended (returns Object, supports chained access)
Debug.Print .Root("name") 'Output: John
Debug.Print .Root("age") 'Output: 25
Debug.Print .Root("city") 'Output: Beijing
'Or use the default member feature (more concise)
Debug.Print $("name") 'Output: John
End With
End Sub3.2 Parsing Nested Objects and Arrays (Using Root Chained Access)
vb
Private Sub ParseComplexJson()
Dim JsonText As String
JsonText = "{""code"":200,""data"":[{""id"":1,""name"":""Project 1""},{""id"":2,""name"":""Project 2""}]}"
With New VBMANLIB.cJson
.Decode JsonText
'Access top-level fields using Root
Debug.Print .Root("code") 'Output: 200
'Use Root chained access for array elements (Note: index starts from 1)
Debug.Print .Root("data")(1)("name") 'Output: Project 1
Debug.Print .Root("data")(2)("name") 'Output: Project 2
'Loop through array (using Root)
Dim Item As Variant
For Each Item In .Root("data")
Debug.Print Item("id") & " - " & Item("name")
Next
End With
End Sub4. File Operations
4.1 Saving JSON to a File
vb
Private Sub SaveJsonToFile()
With VBMAN.Json
.Item("name") = "Test data"
.Item("version") = "1.0"
'Save to file (parameters: path, encoding, indent, display Chinese)
.SaveTo "C:\tmp\data.json", "UTF-8", 2, True
MsgBox "File saved"
End With
End Sub4.2 Loading JSON from a File
vb
Private Sub LoadJsonFromFile()
With New VBMANLIB.cJson
'Load from file
.LoadFrom "C:\tmp\data.json", "UTF-8"
'Use data (access using Root)
MsgBox .Root("name")
End With
'Or directly use (Root is default member)
MsgBox VBMAN.Json.LoadFrom("C:\tmp\data.json")("name")
End Sub5. Using with HTTP Requests
5.1 Sending JSON Data (POST Request)
vb
Private Sub PostJsonData()
'Construct request body
Dim Body As String
With New cJson
.Item("sysStuffCode") = "TEST001"
.Item("quantity") = 2
With .NewItems("detailList")
Dim i As Long
For i = 0 To 3
With .NewItem()
.Item("test") = 123
.Item("time") = Now()
End With
Next
End With
Body = .Encode()
End With
'Send request
With New cHttpClient
.RequestHeaders.Add "Content-Type", "application/json"
Dim Response As String
Response = .Fetch(ReqPost, "https://api.example.com/submit", Body).ReturnText()
Debug.Print Response
End With
End Sub5.2 Parsing Returned JSON Data
vb
Private Sub ParseApiResponse()
With VBMAN.HttpClient.Fetch(ReqGet, "https://api.example.com/data").ReturnJson()
'Format and display returned content
Text1.Text = .Encode(, 2, True)
'Extract data
If .Item("code") = 200 Then
Dim Item As Variant
For Each Item In .Item("data")
List1.AddItem Item("name")
Next
Else
MsgBox .Item("message")
End If
End With
End Sub6. Common Error Handling
vb
Private Sub SafeJsonOperation()
On Error GoTo ErrorHandler
Dim Json As New cJson
'Parse potentially malformed JSON
Json.Decode Text1.Text
'Check if parsing was successful
If Json.LastSuccess Then
MsgBox "Parse successful: " & Json.Item("name")
Else
MsgBox "Parse failed: " & Json.LastError
End If
Exit Sub
ErrorHandler:
MsgBox "Operation error: " & Err.Description
End SubNext Steps
- Check API Reference for complete properties and methods
- Check Code Examples for more usage scenarios