Sample code for 30+ languages & platforms
Rust

JSON: Nested Array

See more JSON Examples

Here we have a JSON object that contains nested arrays. This example demonstrates how to access the contents of the nested arrays.
{
 "numbers" : [ 
    ["even", 2, 4, 6, 8], 
    ["prime", 2, 3, 5, 7, 11, 13] 
  ] }

Chilkat Rust Downloads

Rust

let json = chilkat::JsonObject::new();

// This is the above JSON with whitespace chars removed (SPACE, TAB, CR, and LF chars).
// The presence of whitespace chars for pretty-printing makes no difference to the Load
// method. 
let json_str = "{ \"numbers\" : [ [\"even\", 2, 4, 6, 8], [\"prime\", 2, 3, 5, 7, 11, 13] ] }".to_string();

if json.load(&json_str).is_err() {
    println!("{}", json.last_error_text());
    return;
}

// Get the value of the "numbers" object, which is an array that contains JSON arrays.
let Ok(outer_array) = json.array_of("numbers") else {
    println!("numbers array not found.");
    return;
};

let num_arrays = outer_array.size();

for i in 0..num_arrays {

    let inner_array = outer_array.array_at(i).unwrap();

    // The first item in the innerArray is a string
    println!("{}:", inner_array.string_at(0).unwrap_or_default());

    let num_inner_items = inner_array.size();
    for j in 1..num_inner_items {

        println!("  {}", inner_array.int_at(j));

    }

}