Sample code for 30+ languages & platforms
Rust

JSON Iterate Members

See more JSON Examples

Demonstrates how to loop over the immediate members of a JSON object.

Chilkat Rust Downloads

Rust

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

let json_str = "{ \"id\": 1, \"name\": \"A green door\", \"tags\": [\"home\", \"green\"], \"price\": 125 }".to_string();

if json.load(&json_str).is_err() {
    println!("{}", json.last_error_text());
    return;
}

let num_members = json.size();
for i in 0..num_members {

    let name = json.name_at(i).unwrap_or_default();
    let value = json.string_at(i).unwrap_or_default();

    println!("{}: {}", name, value);

    let i_value = json.int_at(i);
    println!("{} as integer: {}", name, i_value);

}

// Note: The StringAt method returns the value as a string regardless of the type.
// If the value is a JSON array (such as for ["home", "green"]), then the JSON encoding
// of the entire array is returned.

// The IntAt method returns the value as an integer.  If the value does not convert to 
// an integer, then 0 is returned.