Basic JSON Parser Example

Read JSON files and extract tokens

API Features Demonstrated

Platforms

This app works on:

Requirements and Prerequisites

This app requires a ZentriOS device.

Description

This app demonstrates the JSON parsing utility API.

Usage Instructions

No interaction is required. The first part of the output is as follows:

> JSON Parser demo starting...


Parsing: json/example1.json
[Closed: 0]
> JSON file contents:
Object: 2
String: markers
Array: 3
Object: 16
String: point
Array: 2
Float: 40.266044
Float: -74.718479
String: homeTeam
String: Lawrence Library
String: awayTeam
String: LUGip
String: markerImage
String: images/red.png
String: information
String: Linux users group meets second Wednesday of each month.
String: fixture
String: Wednesday 7pm
String: capacity
String:
String: previousScore
String:
...

This corresponds to the first part of the json/example1.json file:

{"markers": [
        {
            "point":[40.266044,-74.718479], 
            "homeTeam":"Lawrence Library",
            "awayTeam":"LUGip",
            "markerImage":"images/red.png",
            "information": "Linux users group meets second Wednesday of each month.",
            "fixture":"Wednesday 7pm",
            "capacity":"",
            "previousScore":""
        },
...
] }

After completing the sequence of parsing steps, the app exits and the device reboots.

Implementation

parse_example1.c

The example source parse_example1.c is a basic JSON parsing example. It uses a statically allocated buffer for chunked reading of the JSON file.

parse_example2.c

The example source parse_example2.c demonstrates having the entire JSON string pre-read into a buffer. The json buffer is then parsed and the tokens are looked up by their string value.

parse_example3.c

The example source parse_example3.c demonstrates reading a JSON file in chunks and keeping only a subset of the tokens.

The call to json_parse_context_init passes a configuration specifying the file_reader callback and a json_token_callback function, called before a token is allocated. It tests the token for meeting certain criteria and sets values of the keys within the context structure. It returns ZOS_SUCCESS if the token is to be kept and ZOS_ABORTED if the token is to be discarded.

parse_example9.c

The example source parse_example9.c demonstrates reading nested values from a JSON structure.

Source

See:

See also: