array_to_json() 是一个用于将 数组 转换为JSON表示形式的系统函数。
array_to_json() 是在 PostgreSQL 9.2 中添加的。
用法
array_to_json (anyarray[,boolean] ) →json
如果可选的 boolean 参数设置为 TRUE,则会在顶级数组元素之间添加换行符。
变更历史
- PostgreSQL 9.2
- 添加 (提交 39909d1d)
示例
array_to_json() 的基本用法示例
postgres=# SELECT array_to_json(ARRAY[1,2,3]); array_to_json --------------- [1,2,3] (1 row) postgres=# SELECT array_to_json(ARRAY[ [1,2,3],[4,5,6] ]); array_to_json ------------------- [[1,2,3],[4,5,6]] (1 row) postgres=# SELECT array_to_json(ARRAY[ [1,2,3],[4,5,6] ], TRUE); array_to_json --------------- [[1,2,3], + [4,5,6]] (1 row)
参考资料
- PostgreSQL文档: JSON创建函数
