I have a json string as
{
"models": [
{
"id": 1011880,
"quantity": "6.118954",
"price": "409.78",
"taker_side": "sell",
"created_at": 1457370745
},
{
"id": 1011791,
"quantity": "1.15",
"price": "409.12",
"taker_side": "sell",
"created_at": 1457365585
}
],
"current_page": 2,
"total_pages": 1686
}
Without defining the whole structure like current_page and total_pages. Can I get the content which is an Array from models and do into_iter() with it?
Yes, you can define the parts you need.
use serde_derive::Deserialize;
#[derive(Deserialize)]
struct Response {
models: Vec<Model>,
}
#[derive(Deserialize)]
struct Model {
id: u64,
}
fn main() -> serde_json::Result<()> {
let j = r#"
{
"models": [
{
"id": 1011880,
"quantity": "6.118954",
"price": "409.78",
"taker_side": "sell",
"created_at": 1457370745
},
{
"id": 1011791,
"quantity": "1.15",
"price": "409.12",
"taker_side": "sell",
"created_at": 1457365585
}
],
"current_page": 2,
"total_pages": 1686
}"#;
let resp: Response = serde_json::from_str(j)?;
for model in resp.models {
println!("id={}", model.id);
}
Ok(())
}
Thank you!
Most helpful comment
Yes, you can define the parts you need.