Skip to content

VBMAN JSON Data Processing Example

Overview

This example demonstrates how to process JSON data using the VBMAN framework, including JSON creation, parsing, modification, and serialization operations. VBMAN provides a simple and easy-to-use JSON API.

Project Structure

Json/
  ├── Form1.frm      # Main form containing JSON operation example code
  ├── Form1.frx      # Form resource file
  └── VBMAN_DEMOS.vbp # VB6 project file

Core Code Analysis

1. JSON Object Creation and Property Setting

vb
With VBMAN.Json
    .Item("a") = 1                'Set numeric type
    .Item("b") = "dengwei"        'Set string type
    
    'Create nested array
    With .NewItems("c")           
        For i = 0 To 3
            With .NewItem()       'Add array element
                .Item("d") = Now()
                .Item("e") = 34 + i
                .Item("f") = "Test content: " & i
            End With
        Next
    End With
End With

2. JSON File Operations

vb
'Save JSON to file
.SaveTo "c:\tmp\demo.json", , 2   'Third parameter is formatting indent spaces

'Load JSON from file
With VBMAN.Json.LoadFrom("c:\tmp\demo.json")
    MsgBox .Root("b")             'Access top-level property
    MsgBox .Root("c")(1)("f")     'Access nested property
End With

3. JSON Data Access

vb
'Using array index (starting from 1)
MsgBox json("c")(2)("f")

'Using For Each iteration
Dim x As Variant
For Each x In json("c")
    Debug.Print x("f")
Next

'Using index iteration
For i = 1 To json("c").Count
    Debug.Print json("c")(i)("f")
Next

4. JSON Serialization

vb
'Convert JSON to formatted string
Text1.Text = json.Encode(, 2, True)  
'Parameters:
'2nd parameter: indent spaces
'3rd parameter: use Unicode

Feature Description

  1. JSON Data Creation

    • Support for various data types (numbers, strings, booleans, etc.)
    • Support for nested objects and arrays
    • Chainable API operations
    • Automatic type conversion
  2. JSON Data Access

    • Access using keys or indices
    • Support for multi-level nested access
    • Support for array iteration
    • Support for collection operations
  3. JSON Serialization

    • Formatted output
    • Unicode support
    • Custom indentation
    • File I/O support

Technical Points

  1. Object-oriented JSON API design
  2. Support for complex nested data structures
  3. Complete file operation support
  4. Flexible iteration methods
  5. Formatted output control

Use Cases

  1. Web API data exchange
  2. Configuration file processing
  3. Data serialization
  4. Frontend-backend data communication

Extension Suggestions

  1. Add JSON Schema validation
  2. Add JSON Path querying
  3. Implement JSON data compression
  4. Add more helper functions
  5. Implement JSON data comparison functionality

Base on VB6 component release