我正在尝试使用Rust中的serde_json
解析以下松散格式的JSON文件:
{
"Source_n": {
"Destination_n": {
"distance": 2,
"connections": [
{
"color": "Any",
"locomotives": 0,
"tunnels": 0
}
]
}
...
其中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,
}
当我试图将JSON作为L0对象读取时,我在这一行上遇到了紧急情况:
let data: L0 = serde_json::from_str(&route_file_as_string).unwrap();
惊恐:
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/ticket-to-ride`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("missing field `routes`", line: 1889, column: 1)', src/route.rs:39:64
stack backtrace:
0: rust_begin_unwind
at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/std/src/panicking.rs:517:5
1: core::panicking::panic_fmt
at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/core/src/panicking.rs:101:14
2: core::result::unwrap_failed
at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/core/src/result.rs:1617:5
3: core::result::Result<T,E>::unwrap
at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/core/src/result.rs:1299:23
4: ticket_to_ride::route::route_file_to_L0
at ./src/route.rs:39:20
5: ticket_to_ride::route::routes_from_file
at ./src/route.rs:44:33
6: ticket_to_ride::main
at ./src/main.rs:6:5
7: core::ops::function::FnOnce::call_once
at /rustc/59eed8a2aac0230a8b53e89d4e99d55912ba6b35/library/core/src/ops/function.rs:227:5
我能够读取JSON并将其转换为
HashMap<String, Value>
对象,但是当我尝试在更低的层次上工作时,就会出现错误。它似乎正在寻找一个名为routes
的键,但我实际上想要的只是类似于Python中嵌套读取JSON的方式来读取嵌套的HashMap。有什么建议吗?使用这个库进行我的尝试是否合理?
routes
”),您正在尝试反序列化一个仅包含routes
元素的L0
,但您试图反序列化的字符串中缺少该元素。您告诉 serde 哪个元素应反序列化到哪个结构成员的方式是通过给它们相同的名称。 - hkBst#[serde(transparent)]
,或者在哈希映射字段上使用#[serde(flatten)]
。 - Sven Marnach#[serde(rename = "…")]
. 如果你不想为JSON使用Rust结构,则可以使用serde_json :: Value
。 - CaesarHashMap<String, HashMap<String, L2>>
,而不需要定义 L0 和 L1。可能还可以为方便起见设置别名。 - Masklinn