Json值可以包含字符串值.例如.:
postgres=# SELECT to_json('Some "text"'::TEXT);
to_json
-----------------
"Some \"text\""
Run Code Online (Sandbox Code Playgroud)
如何将该字符串提取为postgres文本值?
::TEXT
不起作用.它返回引用的json,而不是原始字符串:
postgres=# SELECT to_json('Some "text"'::TEXT)::TEXT;
to_json
-----------------
"Some \"text\""
Run Code Online (Sandbox Code Playgroud)
谢谢.
PS我正在使用PostgreSQL 9.3
Ian*_*thy 118
在9.4.4使用#>>
运算符为我工作:
select to_json('test'::text) #>> '{}';
Run Code Online (Sandbox Code Playgroud)
要与表列一起使用:
select jsoncol #>> '{}' from mytable;
Run Code Online (Sandbox Code Playgroud)
小智 45
PostgreSQL中没有办法解构标量JSON对象.因此,正如你指出的那样,
select length(to_json('Some "text"'::TEXT) ::TEXT);
Run Code Online (Sandbox Code Playgroud)
是15岁,
诀窍是将JSON转换为一个JSON元素的数组,然后使用提取该元素->>
.
select length( array_to_json(array[to_json('Some "text"'::TEXT)])->>0 );
Run Code Online (Sandbox Code Playgroud)
将返回11.
cur*_*oul 11
->>对我有用。
postgres 版本:
<postgres.version>11.6</postgres.version>
Run Code Online (Sandbox Code Playgroud)
询问:
select object_details->'valuationDate' as asofJson, object_details->>'valuationDate' as asofText from MyJsonbTable;
Run Code Online (Sandbox Code Playgroud)
输出:
asofJson asofText
"2020-06-26" 2020-06-26
"2020-06-25" 2020-06-25
"2020-06-25" 2020-06-25
"2020-06-25" 2020-06-25
Run Code Online (Sandbox Code Playgroud)
好奇先生对此也很好奇。除了#>> '{}'
操作符之外,在 9.6+ 中可以使用操作符获取 jsonb 字符串的值->>
:
select to_jsonb('Some "text"'::TEXT)->>0;
?column?
-------------
Some "text"
(1 row)
Run Code Online (Sandbox Code Playgroud)
如果有一个 json 值,那么解决方案是先转换成 jsonb:
select to_json('Some "text"'::TEXT)::jsonb->>0;
?column?
-------------
Some "text"
(1 row)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
71649 次 |
最近记录: |