🦀 JSON 转 Rust 结构体(serde)JSON to Rust Struct (serde)
根据 JSON 生成 Rust 结构体,带 serde 的 Serialize / Deserialize 派生和 #[serde(rename)] 属性;可空或缺失字段用 Option,混合类型用 serde_json::Value。Generate Rust structs from JSON with serde Serialize/Deserialize derives and #[serde(rename)] attributes; nullable or missing fields use Option and mixed types use serde_json::Value.
使用说明与常见问题How-to & FAQ
「JSON 转 Rust 结构体(serde)」是什么?What is the JSON to Rust Struct (serde) tool?
根据 JSON 生成 Rust 结构体,带 serde 的 Serialize / Deserialize 派生和 #[serde(rename)] 属性;可空或缺失字段用 Option,混合类型用 serde_json::Value。Generate Rust structs from JSON with serde Serialize/Deserialize derives and #[serde(rename)] attributes; nullable or missing fields use Option and mixed types use serde_json::Value.
如何使用「JSON 转 Rust 结构体(serde)」?How do I use the JSON to Rust Struct (serde)?
粘贴 JSON,选择整数类型,下方生成 Rust 代码。在 Cargo.toml 加上 serde(features = ["derive"])和 serde_json,用 serde_json::from_str::
键名是 Rust 关键字怎么办?What if a key is a Rust keyword?
例如 "type" 会生成原始标识符 r#type,序列化时仍使用原来的键名;含连字符或驼峰的键会转成 snake_case 并加 #[serde(rename = "...")]。A key such as "type" becomes the raw identifier r#type and still serializes under its original name; hyphenated or camelCase keys become snake_case with #[serde(rename = "...")].
为什么有的字段带 skip_serializing_if?Why do some fields have skip_serializing_if?
在数组的部分对象中缺失的字段会用 Option 并加上 #[serde(default, skip_serializing_if = "Option::is_none")],这样反序列化缺字段不会报错,序列化时也不会输出 null。Fields missing from some objects use Option with #[serde(default, skip_serializing_if = "Option::is_none")], so missing keys deserialize fine and None is not written back as null.
「JSON 转 Rust 结构体(serde)」免费吗?数据会上传吗?Is the JSON to Rust Struct (serde) free? Is my data uploaded?
「JSON 转 Rust 结构体(serde)」完全免费,无需注册或安装,手机和电脑都能用;所有处理都在你的浏览器本地完成,输入的内容和文件不会上传到服务器。The JSON to Rust Struct (serde) is completely free with no sign-up or install and works on phones and desktops; everything runs locally in your browser and nothing you enter is uploaded.