Rust Requires Chilkat v11.1.0+
Rust
Create more Complex JSON Document
See more JSON Examples
Sample code to create the following JSON document:
{
"Title": "The Cuckoo's Calling",
"Author": "Robert Galbraith",
"Genre": "classic crime novel",
"Detail": {
"Publisher": "Little Brown",
"Publication_Year": 2013,
"ISBN-13": 9781408704004,
"Language": "English",
"Pages": 494
},
"Price": [
{
"type": "Hardcover",
"price": 16.65
},
{
"type": "Kindle Edition",
"price": 7.00
}
]
}
Chilkat Rust Downloads
let json = chilkat::JsonObject::new();
// The only reason for failure in the following lines of code would be an out-of-memory condition..
// An index value of -1 is used to append at the end.
let index = -1;
let _ = json.add_string_at(-1, "Title", "The Cuckoo's Calling").is_ok();
let _ = json.add_string_at(-1, "Author", "Robert Galbraith").is_ok();
let _ = json.add_string_at(-1, "Genre", "classic crime novel").is_ok();
// Let's create the Detail JSON object:
let detail = chilkat::JsonObject::new();
let _ = json.append_object2("Detail", &detail);
let _ = detail.add_string_at(-1, "Publisher", "Little Brown").is_ok();
let _ = detail.add_int_at(-1, "Publication_Year", 2013).is_ok();
let _ = detail.add_number_at(-1, "ISBN-13", "9781408704004").is_ok();
let _ = detail.add_string_at(-1, "Language", "English").is_ok();
let _ = detail.add_int_at(-1, "Pages", 494).is_ok();
// Add the array for Price
let a_price = chilkat::JsonArray::new();
let _ = json.append_array2("Price", &a_price);
// Entry in aPrice will be a JSON object.
// Append a new/empty ojbect to the end of the aPrice array.
let price_obj = chilkat::JsonObject::new();
let _ = a_price.add_object_at2(-1, &price_obj);
let _ = price_obj.add_string_at(-1, "type", "Hardcover").is_ok();
let _ = price_obj.add_number_at(-1, "price", "16.65").is_ok();
let _ = a_price.add_object_at2(-1, &price_obj);
let _ = price_obj.add_string_at(-1, "type", "Kindle Edition").is_ok();
let _ = price_obj.add_number_at(-1, "price", "7.00").is_ok();
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());