小编A. *_*ias的帖子

解析嵌套的 JSON 对象

我正在尝试在 Rust 中使用以下松散格式解析 JSON 文件serde_json

{
  "Source_n": {
    "Destination_n": {
      "distance": 2,
      "connections": [
        {
          "color": "Any",
          "locomotives": 0,
          "tunnels": 0
        }
      ]
    }
...
Run Code Online (Sandbox Code Playgroud)

其中SourceDestination可以是任意数量的键(链接到完整文件)。

我创建了以下结构来尝试反序列化 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 作为 …

json rust serde-json

3
推荐指数
1
解决办法
8030
查看次数

标签 统计

json ×1

rust ×1

serde-json ×1