I have a JSON object that contains a string that itself is a JSON object. How can I deserialize it?
I'd like to be able to do something like:
#[derive(Deserialize)]
struct B {
c: String,
d: u32,
}
#[derive(Deserialize)]
struct Test {
a: String,
b: B,
}
let object: Test = serde_json::from_str(
r#"
{
"a": "string",
"b": "{\"c\":\"c_string\",\"d\":1234}"
}
"#,
)
.unwrap();
But this panics with invalid type: string "{\"c\":\"c_string\",\"d\":1234}", expected struct B
You can use the serde_with
crate with json::nested
:
#[derive(Deserialize)]
struct Test {
a: String,
#[serde(with = "serde_with::json::nested")]
b: B,
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With