使用 Oracle APEX_JSON 生成具有多个函数的 JSON
Generating JSON with multiple functions using Oracle APEX_JSON
目前我正在使用 oracle 生成一些 json 用于后端目的的数据,我正在努力处理必须手动处理的复杂和重复的结构。
例如我有这个对象数组:
{
"infoColumnsWidgets": [
{
"widgetNamespace": "mot",
"widgetName": "info_column",
"orderNumber": 1,
"navigateToPage": null,
"widgetData": {
"title": "Fact",
"textPattern": "$v0",
"values": [
{
"id": "v0",
"type": "int",
"value": "200000"
}
]
}
},
{
"widgetNamespace": "mot",
"widgetName": "info_column",
"orderNumber": 2,
"navigateToPage": null,
"widgetData": {
"title": "Plan",
"textPattern": "$v0",
"values": [
{
"id": "v0",
"type": "int",
"value": "200000"
}
]
}
},
{
"widgetNamespace": "mot",
"widgetName": "info_column",
"orderNumber": 3,
"navigateToPage": null,
"widgetData": {
"title": "Prognosis",
"textPattern": "$v0",
"values": [
{
"id": "v0",
"type": "int",
"value": "100"
}
]
}
}
]
}
当然,我在循环中生成它,但这种结构经常出现,我更愿意将它放入某个函数中以执行以下操作:
function f_getTest return clob as
v_res clob;
begin
apex_json.initialize_clob_output;
apex_json.open_object;
apex_json.open_object('infoColumnsWidgets');
for rec in (select * from some_table_data)
loop
apex_json.write_raw(f_getWidgetJson(rec.param));
end loop;
apex_json.close_object;
apex_json.close_all;
v_res := apex_json.get_clob_output;
apex_json.free_output;
return v_res;
end;
但据我所知,无法使用 apex_json 将一个 json 放入另一个。我可以尝试一些奇怪的变通方法,放置一些占位符并将它们替换为最终的 clob 但不,我不想,请不要让我那样做。
非常欢迎任何想法
这有帮助吗?我从 oracle-base 中获取了示例,并将正文代码移到了一个单独的过程中。在下面的示例中,它是一个内联过程,但没有什么能阻止您将其放入独立过程或包中。
DECLARE
PROCEDURE dept_object
IS
l_cursor SYS_REFCURSOR;
BEGIN
OPEN l_cursor FOR
SELECT d.dname AS "department_name",
d.deptno AS "department_number",
CURSOR(SELECT e.empno AS "employee_number",
e.ename AS "employee_name"
FROM emp e
WHERE e.deptno = d.deptno
ORDER BY e.empno) AS "employees"
FROM dept d
ORDER BY d.dname;
APEX_JSON.open_object;
APEX_JSON.write('departments', l_cursor);
APEX_JSON.close_object;
END;
BEGIN
APEX_JSON.initialize_clob_output;
dept_object;
DBMS_OUTPUT.put_line(APEX_JSON.get_clob_output);
APEX_JSON.free_output;
END;
/
目前我正在使用 oracle 生成一些 json 用于后端目的的数据,我正在努力处理必须手动处理的复杂和重复的结构。
例如我有这个对象数组:
{
"infoColumnsWidgets": [
{
"widgetNamespace": "mot",
"widgetName": "info_column",
"orderNumber": 1,
"navigateToPage": null,
"widgetData": {
"title": "Fact",
"textPattern": "$v0",
"values": [
{
"id": "v0",
"type": "int",
"value": "200000"
}
]
}
},
{
"widgetNamespace": "mot",
"widgetName": "info_column",
"orderNumber": 2,
"navigateToPage": null,
"widgetData": {
"title": "Plan",
"textPattern": "$v0",
"values": [
{
"id": "v0",
"type": "int",
"value": "200000"
}
]
}
},
{
"widgetNamespace": "mot",
"widgetName": "info_column",
"orderNumber": 3,
"navigateToPage": null,
"widgetData": {
"title": "Prognosis",
"textPattern": "$v0",
"values": [
{
"id": "v0",
"type": "int",
"value": "100"
}
]
}
}
]
}
当然,我在循环中生成它,但这种结构经常出现,我更愿意将它放入某个函数中以执行以下操作:
function f_getTest return clob as
v_res clob;
begin
apex_json.initialize_clob_output;
apex_json.open_object;
apex_json.open_object('infoColumnsWidgets');
for rec in (select * from some_table_data)
loop
apex_json.write_raw(f_getWidgetJson(rec.param));
end loop;
apex_json.close_object;
apex_json.close_all;
v_res := apex_json.get_clob_output;
apex_json.free_output;
return v_res;
end;
但据我所知,无法使用 apex_json 将一个 json 放入另一个。我可以尝试一些奇怪的变通方法,放置一些占位符并将它们替换为最终的 clob 但不,我不想,请不要让我那样做。
非常欢迎任何想法
这有帮助吗?我从 oracle-base 中获取了示例,并将正文代码移到了一个单独的过程中。在下面的示例中,它是一个内联过程,但没有什么能阻止您将其放入独立过程或包中。
DECLARE
PROCEDURE dept_object
IS
l_cursor SYS_REFCURSOR;
BEGIN
OPEN l_cursor FOR
SELECT d.dname AS "department_name",
d.deptno AS "department_number",
CURSOR(SELECT e.empno AS "employee_number",
e.ename AS "employee_name"
FROM emp e
WHERE e.deptno = d.deptno
ORDER BY e.empno) AS "employees"
FROM dept d
ORDER BY d.dname;
APEX_JSON.open_object;
APEX_JSON.write('departments', l_cursor);
APEX_JSON.close_object;
END;
BEGIN
APEX_JSON.initialize_clob_output;
dept_object;
DBMS_OUTPUT.put_line(APEX_JSON.get_clob_output);
APEX_JSON.free_output;
END;
/