NetBurner 3.5.7
PDF Version
JSON Array

Example Path: examples/JSON/JsonLexer/JsonArray

JSON Array Application

Overview

This application demonstrates how to use the JSON Lexer library classes ParsedJsonDataSet and JsonRef to create and access JSON array information. The program builds a JSON array with numeric data and then reads back the stored values.

Application Details

Application Name:** JsonArray

The application showcases the fundamental operations of:

  • Building JSON data structures programmatically
  • Storing arrays within JSON datasets
  • Accessing and iterating through JSON array elements
  • Converting JSON data to native data types

Code Structure

Main Components

BuildJsonArray()

This function demonstrates the JSON array creation process:

  • Initializes the JSON data set building process
  • Creates an array named "FirstArray"
  • Populates the array with integers 0-9
  • Finalizes the array and dataset construction

ReadJsonArray()

This function shows how to access and iterate through JSON array data:

  • Retrieves a reference to the "FirstArray"
  • Iterates through array elements using index-based access
  • Converts JSON values to native double format
  • Prints each array element with its index

UserMain()

The main application entry point that:

  • Initializes the system
  • Enables system diagnostics
  • Waits for network connectivity (5 seconds)
  • Orchestrates the build and read operations
  • Displays the complete JSON object structure
  • Enters an infinite loop with 1-second delays

Expected Output

When executed, the application will:

  1. Display "Application started" message
  2. Show "Build JSON Object:" followed by the complete JSON structure
  3. Display each array element in the format: " [index]: [value]"
  4. Show "End of Array Reached" when iteration completes

Example output format:

Application started
Build JSON Object:
[JSON structure display]
0: 0
1: 1
2: 2
...
9: 9
End of Array Reached

Dependencies

The application requires:

  • init.h - System initialization functions
  • json_lexer.h - JSON parsing and manipulation library
  • Network stack (uses DHCP and network timing functions)
  • RTOS functionality (uses OSTimeDly and TICKS_PER_SECOND)

Key Features

  • Dynamic JSON Construction: Build JSON arrays programmatically at runtime
  • Type Safety: Automatic conversion between JSON and native data types
  • Iterator Support: Easy traversal of array elements using index notation
  • Validation: Built-in validity checking for array elements
  • Debug Output: Complete JSON object structure printing capability

Usage Notes

  • The application waits for network initialization before proceeding
  • System diagnostics are enabled for development/debugging purposes
  • The main loop runs indefinitely with 1-second intervals
  • Array elements are accessed using standard bracket notation
  • The Valid() method is used to detect the end of array iteration

Technical Implementation

The application leverages the JSON Lexer library's object-oriented approach to JSON manipulation, using:

  • ParsedJsonDataSet for the main JSON container
  • JsonRef for element references and access
  • Method chaining for efficient array construction
  • Implicit type conversion for data access