Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.1.0+

Create a JSON Array of Objects

See more JSON Examples

Demonstrates how to create a JSON array of objects.

Chilkat Rust Downloads

Rust

let arr = chilkat::JsonArray::new();

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

// Create a new and empty JSON object in the 1st position of the JSON array 
// and return the reference in the last argument.
let _ = arr.add_object_at2(0, &obj);
let _ = obj.update_string("Name", "Otto");
let _ = obj.update_int("Age", 29);
let _ = obj.update_bool("Married", false);

// Create a new and empty JSON object in the 2nd position of the JSON array 
// and return the reference in the last argument.
let _ = arr.add_object_at2(1, &obj);
let _ = obj.update_string("Name", "Connor");
let _ = obj.update_int("Age", 43);
let _ = obj.update_bool("Married", true);

// Create a new and empty JSON object in the 3rd position of the JSON array 
// and return the reference in the last argument.
let _ = arr.add_object_at2(2, &obj);
let _ = obj.update_string("Name", "Ramona");
let _ = obj.update_int("Age", 34);
let _ = obj.update_bool("Married", true);

// Examine what we have:
arr.set_emit_compact(false);
println!("{}", arr.emit().unwrap_or_default());

// The output is:

// [
//   {
//     "Name": "Otto",
//     "Age": 29,
//     "Married": false
//   },
//   {
//     "Name": "Connor",
//     "Age": 43,
//     "Married": true
//   },
//   {
//     "Name": "Ramona",
//     "Age": 34,
//     "Married": true
//   }
// ]