Can't parse Serde_json to Nested structs

{"ret_code":0,"ret_msg":"OK","ext_code":"","ext_info":"","result":[{"order_id":"b683254d-8194-43cb-a26b-8f11779c6ce0
","user_id":3403345,"symbol":"BTCUSDT","side":"Buy","order_type":"Limit","price":4100,"qty":0.002,"time_in_force":"G
oodTillCancel","order_status":"New","last_exec_price":0,"cum_exec_qty":0,"cum_exec_value":0,"cum_exec_fee":0,"order_
link_id":"","reduce_only":false,"close_on_trigger":false,"created_time":"2021-07-15T21:40:36Z","updated_time":"2021-
07-15T21:48:52Z","take_profit":0,"stop_loss":0,"tp_trigger_by":"UNKNOWN","sl_trigger_by":"UNKNOWN"}],"time_now":"162
6408503.618801","rate_limit_status":599,"rate_limit_reset_ms":1626408503610,"rate_limit":600}
pub struct QueryActiveOrderRealTime {
    ret_code: usize,
    ret_msg: String,
    ext_code: String,
    ext_info: String,
    result: Option<Vec<QueryActiveOrderRealTimeData>>,
    time_now: String,
    rate_limit_status: usize,
    rate_limit_reset_ms: usize,
    rate_limit: usize,
}
impl ResultBody for QueryActiveOrderRealTime {}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct QueryActiveOrderRealTimeData {
    order_id: String,
    user_id: usize,
    symbol: Symbol,
    side: Side,
    order_type: OrderType,
    price: f64,
    qty: f64,
    time_in_force: TimeInForce,
    order_status: OrderStatus,
    last_exec_price: f64,
    cum_exec_qty: f64,
    cum_exec_value: f64,
    cum_exec_fee: f64,
    order_link_id: String,
    reduce_only: bool,
    close_on_trigger: bool,
    created_time: String,
    updated_time: String,
    take_profit: f64,
    stop_loss: f64,
    tp_trigger_by: String,
    sl_trigger_by: String,
}
impl ResultBody for QueryActiveOrderRealTimeData {}

I tried to use these two Struct to parse the json. But I got this ` Error("invalid type: map, expected usize",

It parsed well after I replaced all those custom types with String(I guess they're enums in your actual code but IDK). Maybe you got different json structure on real case?

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cb7dc4ed9cb4549e161c63c996ff57eb

1 Like

Got it. Thank you very much!