json - 将 json 数组插入 postgres json []

标签 json postgresql insert

如何将数组 ["a","b","c"] 插入到测试中?

create table test( f json [] );

我试过了

insert into test (f) values('{\"a\",\"b\",\"c\"}');

但是当我选择它时会显示转义反斜杠。 如果不转义,它根本不起作用。 ( token “a”无效)

select * from test;

f
----
{"\"a\"","\"b\"","\"c\""}

最佳答案

我想您只想插入一个 json 数组 (json) 而不是一个 json 数组 (json[]):

create table test (f json);
insert into test (f) values(to_json(array['a','b','c']));
select * from test;
       f       
---------------
 ["a","b","c"]

如果你想要一个 json 数组:

create table test (f json[]);
insert into test (f) values
    (array[to_json('a'::text),to_json('b'::text),to_json('c'::text)]);
select * from test;
             f             
---------------------------
 {"\"a\"","\"b\"","\"c\""}

关于json - 将 json 数组插入 postgres json [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33696031/

相关文章:

python-3.x - 在遍历非常大的 ndarray 时,是否有更快的方法将记录插入 postgresql 数据库?

postgresql - 在 Postgres 中返回负匹配

SQL Access INSERT INTO 失败

mysql - 仅当语句为 true 时才插入

python - TypeError : array([ 0.]) 不可 JSON 序列化

java - Jackson 使用时间戳字段反序列化 JSON

postgresql - 为什么 psycopg2 对重复的 SELECT 返回相同的结果?

javascript - 可以缓存 JSON 以提高性能/加载时间吗?

ruby-on-rails - 使用 map 方法格式化 JSON 输出 - Rails 3

mysql - 如何将两条 SQL 语句合并为一条语句?