如何使用Postgres将JSON数据插入到我的表中

Din*_*avi 2 sql postgresql json jsp servlets

我有2列的客户表

CREATE TABLE clients
(
    client_id    serial primary key,
    name        VARCHAR(40) not null
)
Run Code Online (Sandbox Code Playgroud)

我有一个JSON数据像

[{client_id:"1",name:"Rick"},{client_id:"2",name:"Carlin"}]
Run Code Online (Sandbox Code Playgroud)

现在,我需要使用此json进行解析并将其插入到我的客户表中。我如何使用jsp servletpostgres数据库做到这一点。

poz*_*ozs 5

如果要在PostgreSQL(9.3+)上执行此操作,可以使用以下json_populate_recordset函数:

insert into clients
select *
from json_populate_recordset(
  null::clients,
  '[{client_id:"1",name:"Rick"},{client_id:"2",name:"Carlin"}]'
)
Run Code Online (Sandbox Code Playgroud)

虽然,将值手动插入串行列通常不是一个好主意