Skip to content

Commit da86d30

Browse files
authored
Create parsing-json-in-rust.rs
1 parent 66a11b1 commit da86d30

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

source/parsing-json-in-rust.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Free Flutter Course 💙 https://linktr.ee/vandadnp
2+
// Want to support my work 🤝? https://buymeacoffee.com/vandad
3+
4+
use serde::Deserialize;
5+
use std::fmt;
6+
7+
#[derive(Deserialize, Debug)]
8+
struct Person {
9+
name: String,
10+
age: u32,
11+
}
12+
13+
impl fmt::Display for Person {
14+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15+
write!(f, "Person (name: {}, age: {})", self.name, self.age)
16+
}
17+
}
18+
19+
#[tokio::main]
20+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
21+
let persons = reqwest::Client::builder()
22+
.build()?
23+
.get("http://localhost:5500/apis/persons.json")
24+
.send()
25+
.await?
26+
.json::<Vec<Person>>()
27+
.await?;
28+
println!("Persons are {:?}", persons);
29+
Ok(())
30+
}
31+
32+
/* Cargo.toml dependencies
33+
[dependencies]
34+
reqwest = { version = "0.11", features = ["blocking", "json"] }
35+
tokio = { version = "1", features = ["full"] }
36+
serde = { version = "1.0.136", features = ["derive"] }
37+
*/
38+

0 commit comments

Comments
 (0)