Example Path: examples/JSON/JsonLexer/JsonArrayWithObjects
JSON Array with Objects Example
Overview
This application demonstrates how to use the JSON Lexer classes ParsedJsonDataSet and JsonRef to create and access arrays containing sub-objects. The program creates a JSON array with 10 objects, each containing two key-value pairs, and then reads back the data to display it.
Application Details
Application Name:** JsonArrayWithObjects
The program showcases the following JSON manipulation capabilities:
- Building JSON arrays programmatically
- Adding objects as array elements
- Accessing array elements by index
- Reading object properties within arrays
Code Structure
Main Functions
BuildJsonArray()
Creates a JSON array named "FirstArray" containing 10 objects. Each object has:
- Key "a" with value equal to the loop index (0-9)
- Key "b" with value equal to the loop index multiplied by 10 (0, 10, 20, ..., 90)
ShowObject(JsonRef element)
Displays the contents of a single object by printing its "a" and "b" values.
ReadJsonArrayWithObj()
Iterates through the "FirstArray" and displays each object's contents using the ShowObject() function.
UserMain(void *pd)
Main entry point that:
- Initializes the system
- Enables system diagnostics
- Waits for network connectivity
- Builds the JSON array
- Prints the complete JSON object
- Reads and displays the array contents
- Enters an infinite loop
JSON Structure Created
The application creates a JSON structure similar to:
{
"FirstArray": [
{ "a": 0, "b": 0 },
{ "a": 1, "b": 10 },
{ "a": 2, "b": 20 },
...
{ "a": 9, "b": 90 }
]
}
Dependencies
Usage
The application automatically runs when started and will:
- Display startup message
- Build and print the JSON object
- Read through the array and display each object's contents
- Continue running indefinitely
Key Classes Used
- ParsedJsonDataSet: Main class for building and storing JSON data
- JsonRef: Reference class for accessing JSON elements and navigating the structure