Postgres-转义双引号内的转义双引号

时间:2020-10-19 05:07:07

标签: sql json postgresql

我有一个名为 summary 的表,该表有两列, id SERIAL summary json数组元素。

要插入此表中,我必须遵循以下语法:

insert into summary values (default, '{"{\"index\": -1, \"nivel\": -1, `\"title\": \"hello world\"`, \"children\": []}"}')

但是,如果我想在标题中转义双引号,例如:

insert into summary values (default, '{"{\"index\": -1, \"nivel\": -1, \"title\": \"hello \"world\"\", \"children\": []}"}')

它返回此错误:

ERROR:  invalid input syntax for type json
LINE 1: insert into summary values (default, '{"{\"index\": -1, \"ni...
                                             ^
DETAIL:  Token "world" is invalid.
CONTEXT:  JSON data, line 1: {"index": -1, "nivel": -1, "title": "hello "world...

我该如何正确逃脱?

1 个答案:

答案 0 :(得分:1)

通过在前面加上转义的反斜杠,对双引号进行双引号转义:

\"hello \\\"world\\\"\"