Postgres & Rust R2D2:如何在没有转义双引号的情况下将 array_to_json 获取为 text/string?
Postgres & Rust R2D2: How to get array_to_json as text/string without the escaped double quotes?
我有以下 SQL:
select jsonb_agg(t)
from (
select username, notifications
from useraccountview where username = 'Bobby'
) t;
这让我在 PSQL 中跟随:
[{"username":"Bobby","notifications":0}]
当我输入 ::text
:
select jsonb_agg(t)::text
from (
select username, notifications
from useraccountview where username = 'Bobby'
) t;
我还是一样:
[{"username":"Bobby","notifications":0}]
但是在我的 Rust 应用程序端使用 r2d2_postgres,我得到了一个转义字符串:
"[{\"username\":\"Bobby\",\"notifications\":0}]"
防锈代码:
let qr = conn.query("select jsonb_agg(t)::text from (select username, notifications from useraccount where username = ) t",&[¶m]).unwrap();
let value: &str = qr[0].get(0);
println!("{:?}",value);
输出:
"[{\"username\":\"Bobby\",\"notifications\":0}]"
如何防止双引号转义?
引号仅在打印时转义。它们不会在内存中转义。如果要打印不带引号的值,请使用 println!("{}", value);
。
我有以下 SQL:
select jsonb_agg(t)
from (
select username, notifications
from useraccountview where username = 'Bobby'
) t;
这让我在 PSQL 中跟随:
[{"username":"Bobby","notifications":0}]
当我输入 ::text
:
select jsonb_agg(t)::text
from (
select username, notifications
from useraccountview where username = 'Bobby'
) t;
我还是一样:
[{"username":"Bobby","notifications":0}]
但是在我的 Rust 应用程序端使用 r2d2_postgres,我得到了一个转义字符串:
"[{\"username\":\"Bobby\",\"notifications\":0}]"
防锈代码:
let qr = conn.query("select jsonb_agg(t)::text from (select username, notifications from useraccount where username = ) t",&[¶m]).unwrap();
let value: &str = qr[0].get(0);
println!("{:?}",value);
输出:
"[{\"username\":\"Bobby\",\"notifications\":0}]"
如何防止双引号转义?
引号仅在打印时转义。它们不会在内存中转义。如果要打印不带引号的值,请使用 println!("{}", value);
。