Rust
Rust
JSON: Nested Objects
See more JSON Examples
Here we have a JSON object that contains nested JSON objects. This example demonstrates how to access the contents of the nested objects.
{
"name": "donut",
"image":
{
"fname": "donut.jpg",
"w": 200,
"h": 200
},
"thumbnail":
{
"fname": "donutThumb.jpg",
"w": 32,
"h": 32
}
}
Chilkat Rust Downloads
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 = "{\"name\": \"donut\",\"image\":{\"fname\": \"donut.jpg\",\"w\": 200,\"h\": 200},\"thumbnail\":{\"fname\": \"donutThumb.jpg\",\"w\": 32,\"h\": 32}}".to_string();
if json.load(&json_str).is_err() {
println!("{}", json.last_error_text());
return;
}
// Get the "image" object.
let image_obj = chilkat::JsonObject::new();
let _ = json.object_of2("image", &image_obj);
println!("image: fname={}, width={}, height={}", image_obj.string_of("fname").unwrap_or_default(), image_obj.int_of("w"), image_obj.int_of("h"));
// Get the "thumbnail" object.
let thumb_obj = chilkat::JsonObject::new();
let _ = json.object_of2("thumbnail", &thumb_obj);
println!("thumbnail: fname={}, width={}, height={}", thumb_obj.string_of("fname").unwrap_or_default(), thumb_obj.int_of("w"), thumb_obj.int_of("h"));