Issuing on PostgresSQL 9.2 the following:
在PostgresSQL 9.2上发布以下内容:
CREATE TABLE test (j JSON, ja JSON[]);
INSERT INTO test (j) VALUES('{"name":"Alex", "age":20}' ); -- Works FINE
INSERT INTO test (ja) VALUES( ARRAY['{"name":"Alex", "age":20}', '{"name":"Peter", "age":24}'] ); -- Returns ERROR
The first insert works fine. The second insert returns error: column "ja" is of type json[] but expression is of type text[]
第一个插件工作正常。第二个插入返回错误:列“ja”的类型为json [],但表达式的类型为text []
I can cast type to prevent the error:
我可以转换类型以防止错误:
INSERT INTO test(ja) VALUES( CAST (ARRAY['{"name":"Alex", "age":20}', '{"name":"Peter", "age":24}'] as JSON[]) ); -- Works FINE
My question is if there is a way to avoid casting?
我的问题是,是否有办法避免施法?
1 个解决方案
#1
11
insert into test(ja) values
('{"{\"name\":\"alex\", \"age\":20}", "{\"name\":\"peter\", \"age\":24}"}');
To avoid the confuse escaping cast each string:
为了避免混淆转换每个字符串:
insert into test(ja) values
(array['{"name":"alex", "age":20}'::json, '{"name":"peter", "age":24}'::json]);
Or just cast the array itself:
或者只是投射阵列本身:
insert into test (ja) values
(array['{"name":"alex", "age":20}', '{"name":"peter", "age":24}']::json[]);
#1
11
insert into test(ja) values
('{"{\"name\":\"alex\", \"age\":20}", "{\"name\":\"peter\", \"age\":24}"}');
To avoid the confuse escaping cast each string:
为了避免混淆转换每个字符串:
insert into test(ja) values
(array['{"name":"alex", "age":20}'::json, '{"name":"peter", "age":24}'::json]);
Or just cast the array itself:
或者只是投射阵列本身:
insert into test (ja) values
(array['{"name":"alex", "age":20}', '{"name":"peter", "age":24}']::json[]);