Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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
列表中的Postgres JSON查询_Json_Postgresql - Fatal编程技术网

列表中的Postgres JSON查询

列表中的Postgres JSON查询,json,postgresql,Json,Postgresql,我在匹配表中有以下数据: {"Id":1,"Teams":[{"Name":"TeamA","Players":[{"Name":"AAA"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"CCC"},{"Name":"DDD"}]}],"TeamRank":[1,2]} {"Id":2,"Teams":[{"Name":"TeamA","Players":[{"Name":"CCC"},{"Name":"BBB"}]},{"Name

我在匹配表中有以下数据:

{"Id":1,"Teams":[{"Name":"TeamA","Players":[{"Name":"AAA"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"CCC"},{"Name":"DDD"}]}],"TeamRank":[1,2]}

{"Id":2,"Teams":[{"Name":"TeamA","Players":[{"Name":"CCC"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"AAA"},{"Name":"DDD"}]}],"TeamRank":[1,2]}
最终,我想得到一个独特的团队名称列表

以下是我一直在尝试使用以下方法访问Team Name属性:

SELECT json_array_elements(match->>'Teams') FROM matches
这是回报

ERROR: function json_array_elements(text) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 1560
我还尝试了json_数组元素(match->>“Teams”::json)和json_数组元素(to_json(match->>“Teams”)),但没有成功

但是下面的问题

SELECT match->>'Teams' FROM matches;
返回

"[{"Name":"TeamA","Players":[{"Name":"AAA"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"CCC"},{"Name":"DDD"}]}]"
"[{"Name":"TeamA","Players":[{"Name":"CCC"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"AAA"},{"Name":"DDD"}]}]"

->
操作符将结果作为
文本提供给您,但您希望它保持为
json
。使用
->
,它将为您提供
json

参考:

我相信
json\u数组元素的操作顺序(match->>“Teams”::json)
在运行
->
之前将
Teams
转换为
json
json\u数组元素((match->>'Teams')::json)
应该可以工作,但它只是
->
的一个迂回版本


to_json(匹配->'Teams')
转换为
text
,然后将该文本作为
json
对象提供给您。它不会将
文本
解析回
json

哇,这是一个非常简单的解决方案。是否有任何理由使用文本版本?(>>超过->)有时需要文本,有时需要更多JSON,完全取决于您的用例。当涉及到
NULL
时,会有一些不同的行为-下面是一个例子。