Rust Requires Chilkat v11.0.0+
Rust
Swap JSON Objects
See more JSON Examples
Demonstrates how to swap two JSON objects within a JSON document.Chilkat Rust Downloads
let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
// Load the following JSON:
// {
// "petter": {
// "DOB": "26/02/1986",
// "gender": "male",
// "country": "US"
// },
// "Sara": {
// "DOB": "13/05/1982",
// "gender": "female",
// "country": "FR"
// },
// "Jon": {
// "DOB": "19/03/1984",
// "gender": "male",
// "country": "UK"
// }
// }
if json.load_file("qa_data/json/people.json").is_err() {
println!("{}", json.last_error_text());
return;
}
// Swap the positions of Jon and Sara.
let mut index1 = json.index_of("Jon");
let mut index2 = json.index_of("Sara");
let _ = json.swap(index1, index2);
// We have this now:
println!("{}", json.emit().unwrap_or_default());
// {
// "petter": {
// "DOB": "26/02/1986",
// "gender": "male",
// "country": "US"
// },
// "Jon": {
// "DOB": "19/03/1984",
// "gender": "male",
// "country": "UK"
// },
// "Sara": {
// "DOB": "13/05/1982",
// "gender": "female",
// "country": "FR"
// }
// }
// To swap an inner member:
let json_sara = chilkat::JsonObject::new();
let _ = json.object_of2("Sara", &json_sara);
index1 = json_sara.index_of("DOB");
index2 = json_sara.index_of("country");
let _ = json_sara.swap(index1, index2);
// We now have this:
println!("{}", json.emit().unwrap_or_default());
// {
// "petter": {
// "DOB": "26/02/1986",
// "gender": "male",
// "country": "US"
// },
// "Jon": {
// "DOB": "19/03/1984",
// "gender": "male",
// "country": "UK"
// },
// "Sara": {
// "country": "FR",
// "gender": "female",
// "DOB": "13/05/1982"
// }
// }