所有关于通过POST接收Json的示例都是这样的。Json被接收、反序列化,然后直接使用。
#[derive(Deserialize)] struct Info { username: String, } async fn index(info: web::Json<Info>) -> impl Responder { format!("Welcome {}!", info.username) }
但我不想用 info.username 在此函数中,我需要发送 信息 ,作为另一个函数的参数,作为Info结构体接收。
大致如下:
let resultFromJsonProcess: String = process_input(info);
但它不起作用。主要错误是:
^^^^^^^ expected `Info`, found `Json<Info>`
我以为信息已经是一个格式良好的结构,但我不知道是否需要unwrap()或其他东西。
使用 .into_inner() :
.into_inner()
info.into_inner()
或者, Json 是一个带有公共字段的元组结构:
Json
pub struct Json<T>(pub T);
因此,您可以通过匿名元组字段访问嵌套结构:
async fn index(info: web::Json<Info>) -> impl Responder { format!("Welcome {}!", info.0) }
或者通过模式匹配:
async fn index(Json(info): web::Json<Info>) -> impl Responder { format!("Welcome {}!", info) }