当前位置:网站首页>Rust 入门指南(使用JSON)
Rust 入门指南(使用JSON)
2022-08-09 12:12:00 【InfoQ】
- 读取无类型的 JSON。
- 将 JSON 读取为强类型数据结构。
- 写 JSON 字符串。
serde
serde-json
无类型 JSON
serde_json
cargo new handle_json
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
from_str()
serde_json
serde_json
from_str()
use serde_json::{Value};
fn main() {
let json = r#"
{
"article": "how to work with json in Rust",
"author": "tdep",
"paragraph": [
{
"name": "untyped"
},
{
"name": "strongly typed"
},
{
"name": "writing json"
}
]
}
"#;
let parsed: Value = read_json(json);
println!("\n\n The title of the article is {}", parsed["article"])
}
fn read_json(raw_json:&str) -> Value {
let parsed: Value = serde_json::from_str(raw_json).unwrap();
return parsed
}
read_json
serde_json::from_str()
parsed["article"]
有类型的 JSON
serde
serde
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Paragraph {
name: String
}
#[derive(Serialize, Deserialize)]
struct Article {
article: String,
author: String,
paragraph: Vec<Paragraph>
}
fn main() {
let json = r#"
{
"article": "how to work with json in Rust",
"author": "tdep",
"paragraph": [
{
"name": "untyped"
},
{
"name": "strongly typed"
},
{
"name": "writing json"
}
]
}
"#;
let parsed: Article = read_json_typed(json);
println!("\n\n The name of the first paragraph is: {}", parsed.paragraph[0].name);
}
fn read_json_typed(raw_json: &str) -> Article {
let parsed: Article = serde_json::from_str(raw_json).unwrap();
return parsed
}
serde
Article
struct
parsed.paragraph[0].name
{
"article": "how to work with json in Rust",
"author": "tdep",
"paragraph": [
{
"name": 1
},
{
"name": "strongly typed"
},
{
"name": "writing json"
}
]
}
name
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: integer `1`, expected a string", line: 8, column: 15)', src/main.rs:44:58
“article”
“name”
{
"name": "how to work with json in Rust",
"author": "tdep",
"paragraph": [
{
"name": "untyped"
},
{
"name": "strongly typed"
},
{
"name": "writing json"
}
]
}
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("missing field `article`", line: 17, column: 1)', src/main.rs:44:58
article
写JSON
serde_json::to_string()
Serialize
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Paragraph {
name: String
}
#[derive(Serialize, Deserialize)]
struct Article {
article: String,
author: String,
paragraph: Vec<Paragraph>
}
fn main() {
let article: Article = Article {
article: String::from("how to work with json in Rust"),
author: String::from("tdep"),
paragraph: vec![
Paragraph {
name: String::from("untyped")
},
Paragraph {
name: String::from("strongly typed")
},
Paragraph {
name: String::from("writing json")
}
]
};
let json = serde_json::to_string(&article).unwrap();
println!("the JSON is: {}", json)
}
Article
serde_json::to_string()
the JSON is: {"article":"how to work with json in Rust","author":"tdep","paragraph":[{"name":"untyped"},{"name":"strongly typed"},{"name":"writing json"}]}
serde_json::from_string
serde_json::to_string
serde_json::to_vec
serde_json::to_writer
to_vec
to_writer
边栏推荐
猜你喜欢
合并两个有序列表
两个链表相加
API调用,API传参,面向对接开发,你真的会写接口文档吗?
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
Say goodbye to the AI era of hand looms
JD.com architects tidy up: what are the core technical knowledge points of jvm and performance tuning
K个结点的组内逆序调整
李开复花上千万投的缝纫机器人,团队出自大疆
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
#Internet of Things essay#Xiaoxiong pie equipment development actual combat
随机推荐
ansible-cmdb友好展示ansible收集主机信息
2022牛客多校(六)M. Z-Game on grid
告别手摇织布机的AI时代
ThreadLocal的简单理解
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
Flutter Getting Started and Advanced Tour (3) Text Widgets
WebView注入Js代码实现大图自适应屏幕点击图片预览详情
Intranet penetration tool ngrok usage tutorial
Simple understanding of ThreadLocal
内网穿透工具ngrok使用教程
读写分离后,性能居然提升100%了呀
发明时代,「幂集创新」事关你我
链表噩梦之一?5000多字带你弄清它的来龙去脉
字符串转换整数 (atoi)
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
The grep command Shell regular expressions, the three musketeers
Flutter入门进阶之旅(一)-初识Flutter
微信一面:一致性哈希是什么,使用场景,解决了什么问题?
Common gadgets of Shell (sort, uniq, tr, cut)
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌