Rust
Rust
Loading and Parsing a JSON Array
See more JSON Examples
A JSON array is JSON that begins with "[" and ends with "]". For example, this is a JSON array that contains 3 JSON objects.
[{"name":"jack"},{"name":"john"},{"name":"joe"}]
A JSON object, however, is JSON that begins with "{" and ends with "}". For example, this JSON is an object that contains an array.
{"pets":[{"name":"jack"},{"name":"john"},{"name":"joe"}]}
This example shows how loading a JSON array is different than loading a JSON object.
Chilkat Rust Downloads
let str_json_array = "[{\"name\":\"jack\"},{\"name\":\"john\"},{\"name\":\"joe\"}]".to_string();
let str_json_object = "{\"pets\":[{\"name\":\"jack\"},{\"name\":\"john\"},{\"name\":\"joe\"}]}".to_string();
// A JSON array must be loaded using JsonArray:
let json_array = chilkat::JsonArray::new();
let _ = json_array.load(&str_json_array);
// Examine the values:
let mut i = 0;
while i < json_array.size() {
let json_obj = json_array.object_at(i).unwrap();
println!("{}: {}", i, json_obj.string_of("name").unwrap_or_default());
i = i + 1;
}
// Output is:
// 0: jack
// 1: john
// 2: joe
// A JSON object must be loaded using JsonObject
let json_object = chilkat::JsonObject::new();
let _ = json_object.load(&str_json_object);
// Examine the values:
i = 0;
let num_pets = json_object.size_of_array("pets");
while i < num_pets {
json_object.set_i(i);
println!("{}: {}", i, json_object.string_of("pets[i].name").unwrap_or_default());
i = i + 1;
}
// Output is:
// 0: jack
// 1: john
// 2: joe