0

我正在尝试将 json 字符串反序列化为结构,但无法使其正常工作。我要做的就是打印出第一个与run["meta"]["username"]给定用户名匹配的状态和/或结果。

然而,尽管遵循了给定的例子,我似乎无法让它工作。现在我宁愿用无类型的值来做这件事,因为我不打算存储这些值但是当我这样做时我不能轻易地迭代初始数组,这样做runs.as_array().iter()似乎返回一个包含整个 jsonVec的单个元素String字符串,因此需要添加类型,但现在这也不起作用。

我得到的当前问题是:

error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
  --> src/main.rs:60:30
   |
60 |               let runs: Runs = serde_json::from_str(get_runs(&mut client,
   |  ______________________________^
61 | |                                 &settings["pipeline_id"]).as_str())?;
   | |____________________________________________________________________^ cannot use the `?` operator in a function that returns `()`
   |
   = help: the trait `std::ops::Try` is not implemented for `()`
   = note: required by `std::ops::Try::from_error`

这是没有意义的,因为如果你删除?它然后抱怨runs期待 aRuns而不是收到 a Result,所以我很困惑,它怎么能抱怨它正在返回 aResult然后抱怨它正在返回 a ()

显然我错过了一些东西,我只是没有看到它。

第二个问题是我不能使用.iter()

struct Runs(Vec<Run>);

我知道它是一个包装的 Vec,但它不应该自动委托或其他东西吗?因为我不能使用类型别名或只是这样做:

let runs: Vec<Run> = 

因为我需要:

#[派生(调试,序列化,反序列化)]

在它之前允许它与 serde-rs 一起工作。

那么如何解决这两个问题,让我可以从 json 中获取我需要的数据?

这是一个最小的(生锈)示例:

extern crate serde_json;

#[macro_use]
extern crate serde_derive;

use std::collections::HashMap;


use serde_json::{Value, Error};


#[derive(Debug,Serialize, Deserialize)]
struct Runs(Vec<Run>);

#[derive(Debug,Serialize, Deserialize)]
struct Meta {
    username: String
}

#[derive(Debug,Serialize, Deserialize)]
struct User {
    meta: Meta,
}

#[derive(Debug,Serialize, Deserialize)]
struct Run {
    status: String,
    result: String
    user: User,
}

fn main() {

    let data = r#"
    [
        {
            "id": "577a4dbc3ec144923a02b6f9",
            "url": "https://app.wercker.com/api/v3/runs/577a4dbc3ec144923a02b6f9",
            "branch": "master",
            "commitHash": "cd74a9994712960f19b4b563491f860f98fa7bb5",
            "createdAt": "2016-07-04T11:51:24.851Z",
            "finishedAt": "2016-07-04T11:51:34.284Z",
            "message": "My custom message!",
            "progress": 57,
            "result": "aborted",
            "startedAt": "2016-07-04T11:51:25.996Z",
            "status": "finished",
            "user": {
              "meta": {
                "werckerEmployee": true,
                "username": "tonnu"
              },
              "userId": "549101f36b3ba8733d88ce96",
              "avatar": {
                "gravatar": "bb95c388b227da2ce2f33c0811478095"
              },
              "name": "Toon",
              "type": "wercker"
            },
            "pipeline": {
              "id": "5751377e7e1bd5f17f050bc4",
              "url": "https://app.wercker.com/api/v3/pipelines/5751377e7e1bd5f17f050bc4",
              "createdAt": "2016-06-03T07:53:34.294Z",
              "name": "initial-build",
              "permissions": "public",
              "pipelineName": "initial-build",
              "setScmProviderStatus": true,
              "type": "git"
            }
        }
    ]"#;
    let runs: Runs = serde_json::from_str(data).as_str()?;
    for run in runs.iter() {
        println!("{}","------------");
        println!("{:?}",run)

    }
}
4

0 回答 0