Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust use Postgres JSON属性:无法在Rust类型'alloc::string::string'和Postgres类型'jsonb'之间转换`_Json_Postgresql_Rust_Rust Tokio - Fatal编程技术网

Rust use Postgres JSON属性:无法在Rust类型'alloc::string::string'和Postgres类型'jsonb'之间转换`

Rust use Postgres JSON属性:无法在Rust类型'alloc::string::string'和Postgres类型'jsonb'之间转换`,json,postgresql,rust,rust-tokio,Json,Postgresql,Rust,Rust Tokio,目前我可以使用以下代码,但我不想在postgres查询中强制转换JSON为文本,因为这会增加延迟 async fn reverse_geocode(min : f32, max : f32, pool: &Pool) -> Result<String, PoolError> { let client: Client = pool.get().await?; let sql = format!("select \"json\"

目前我可以使用以下代码,但我不想在postgres查询中强制转换JSON为文本,因为这会增加延迟

async fn reverse_geocode(min : f32, max : f32, pool: &Pool) -> Result<String, PoolError> {
    let client: Client = pool.get().await?;
    let sql = format!("select \"json\"::TEXT from get_data({}, {})", min, max);
    let stmt = client.prepare(&sql).await?;
    let rows = client.query(&stmt, &[]).await?;
    Ok(rows[0].get(0))
}

可以使用什么类型来返回json值而不将其转换为文本?

为了使用json和Jsonb值,您需要在postgres create with中启用该功能 功能=[with-serde_json-1]

然后您可以将返回类型更改为结果

那么你的货物里就有了

[dependencies]
postgres = {version = "0.17.3" , features = ["with-serde_json-1"] }
serde_json = "1.0.56"
在你的主要房间里

async fn reverse_geocode(min : f32, max : f32, pool: &Pool) -> Result<serde_json::Value, PoolError> {
    let client: Client = pool.get().await?;
    let sql = format!("select \"json\" from get_data({}, {})", min, max);
    let stmt = client.prepare(&sql).await?;
    let rows = client.query(&stmt, &[]).await?;
    Ok(rows[0].get(0))
}

为了使用Json和Jsonb值,您需要在postgres create with中启用该功能 功能=[with-serde_json-1]

然后您可以将返回类型更改为结果

那么你的货物里就有了

[dependencies]
postgres = {version = "0.17.3" , features = ["with-serde_json-1"] }
serde_json = "1.0.56"
在你的主要房间里

async fn reverse_geocode(min : f32, max : f32, pool: &Pool) -> Result<serde_json::Value, PoolError> {
    let client: Client = pool.get().await?;
    let sql = format!("select \"json\" from get_data({}, {})", min, max);
    let stmt = client.prepare(&sql).await?;
    let rows = client.query(&stmt, &[]).await?;
    Ok(rows[0].get(0))
}