我正在尝试在 Rust 中使用以下松散格式解析 JSON 文件serde_json:
{
"Source_n": {
"Destination_n": {
"distance": 2,
"connections": [
{
"color": "Any",
"locomotives": 0,
"tunnels": 0
}
]
}
...
Run Code Online (Sandbox Code Playgroud)
其中Source和Destination可以是任意数量的键(链接到完整文件)。
我创建了以下结构来尝试反序列化 JSON:
#[derive(Debug, Deserialize)]
struct L0 {
routes: HashMap<String, L1>,
}
#[derive(Debug, Deserialize)]
struct L1 {
destination_city: HashMap<String, L2>,
}
#[derive(Debug, Deserialize)]
struct L2 {
distance: u8,
connections: Vec<L3>,
}
#[derive(Debug, Deserialize, Clone)]
struct L3 {
color: String,
locomotives: u8,
tunnels: u8,
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将 JSON 作为 …