Some site site.com returns json data, for example:
{
"a": {
"text": ["abc", "xyz", "rty", "io", "f"],
"data": [
[null, "txt", 18.1234, 12345678912345, -0.3]
]
},
"b": {
"text": ["qwe", "rty", "asd"],
"data": [
[11343, 0, 20230407131355]
]
}
}
I want to get it and then save in postgresql db as json record. Try http-client reqwest and serde:
[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0"
main.rs:
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
struct Msg {
a: A,
b: B,
}
#[derive(Serialize, Deserialize, Debug)]
struct A {
text: Vec<String>,
data: Vec<Vec<Option<Datum>>>,
}
#[derive(Serialize, Deserialize, Debug)]
struct B {
text: Vec<String>,
data: Vec<Vec<i64>>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
enum Datum {
Double(f64),
String(String),
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
//
let resp = reqwest::get("https://site.com")
.await?.json::<Msg>().await?;
//
println!("{:?}", resp);
Ok(())
}
i get something like this (println!):
Msg { a: A { text: ["abc", "xyz", ...
data: [[None, Some(String("txt")), Some(Double(18.1234), Some(Double(12345678912345.0)), Some(Double(-0.3))]]
Want to get without A, Some(String / Double as origin.
How to do?