NetBurner 3.5.6
PDF Version
JSON Array with Objects

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:

  1. Initializes the system
  2. Enables system diagnostics
  3. Waits for network connectivity
  4. Builds the JSON array
  5. Prints the complete JSON object
  6. Reads and displays the array contents
  7. 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:

  1. Display startup message
  2. Build and print the JSON object
  3. Read through the array and display each object's contents
  4. 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