某个REST请求的正文定义了以下JSON:
{
"employees": [{
"employee_id": 900,
"first_name": "a",
"last_name": "Sato",
"email": "a.sato",
"hire_date": "01-06-2018",
"job_id": "AD_PRES"
},
{
"employee_id": 901,
"first_name": "b",
"last_name": "Sato",
"email": "aas.sato",
"hire_date": "01-06-2018",
"job_id": "AD_PRES"
}
]
}
有没有办法引用请求体(即上面的JSON字符串)并解析它,以便我可以通过PL / SQL将它们保存到我的数据库中。
APEX版本为5.1.1.00.08
答案 0 :(得分:3)
您可以使用APEX_JSON包执行此操作。
declare
p_json clob := '
{
"employees": [{
"employee_id": 900,
"first_name": "a",
"last_name": "Sato",
"email": "a.sato",
"hire_date": "01-06-2018",
"job_id": "AD_PRES"
},
{
"employee_id": 901,
"first_name": "b",
"last_name": "Sato",
"email": "aas.sato",
"hire_date": "01-06-2018",
"job_id": "AD_PRES"
}
]
}';
type t_employee is record (
employee_id number
,first_name varchar2(255)
,last_name varchar2(255)
,email varchar2(255)
,hire_date date
,job_id varchar2(255)
);
type t_employees is table of t_employee index by pls_integer;
l_employees t_employees;
begin
apex_json.parse(p_json);
for i in 1 .. apex_json.get_count(p_path => 'employees') loop
l_employees(i).employee_id := apex_json.get_varchar2(p_path => 'employees[%d].employee_id', p0 => i);
l_employees(i).first_name := apex_json.get_varchar2(p_path => 'employees[%d].first_name', p0 => i);
l_employees(i).last_name := apex_json.get_varchar2(p_path => 'employees[%d].last_name', p0 => i);
l_employees(i).email := apex_json.get_varchar2(p_path => 'employees[%d].email', p0 => i);
l_employees(i).job_id := apex_json.get_varchar2(p_path => 'employees[%d].job_id', p0 => i);
l_employees(i).hire_date := to_date(apex_json.get_varchar2(p_path => 'employees[%d].hire_date', p0 => i), 'mm-dd-yyyy');
end loop;
-- Do what you want here with l_employees
-- In this example I'm just outputing it.
for i in 1 .. l_employees.count loop
dbms_output.put_line (
'employee_id(' || i || ') => ' || l_employees(i).employee_id || ' / ' ||
'first_name(' || i || ') => ' || l_employees(i).first_name || ' / ' ||
'last_name(' || i || ') => ' || l_employees(i).last_name || ' / ' ||
'email(' || i || ') => ' || l_employees(i).email || ' / ' ||
'job_id(' || i || ') => ' || l_employees(i).job_id || ' / ' ||
'hire_date(' || i || ') => ' || l_employees(i).hire_date
);
end loop;
end;
如果你使用12c,你也可以使用JSON_TABLE,这是一种更好更快的方式。