Ani*_*raj 1 json plsql oracle11g oracle-apex oracle-apex-5.1
我是 ORDS 的新手。我正在使用 APEX_JSON 为源类型为 PL/SQL 的其他服务之一创建一个 json 对象。我正在创建的 json 对象很复杂。如果在创建 json 对象时有任何事情失败,那么我必须返回一个错误,该错误在正文中具有不同的 json 结构。因此,在为错误创建主体之前,我想清除到目前为止我创建的 json。我怎样才能做到这一点。或者有没有其他方法可以实现这一点。
PROCEDURE GETNODES(I_LOCATION IN NUMBER)
V_STATUS NUMBER;
BEGIN
APEX_JSON.OPEN_OBJECT;
APEX_JSON.OPEN_ARRAY('NODES');
FOR nodes in (select NAME FROM NODES where location = I_LOCATION) LOOP
APEX_JSON.OPEN_OBJECT;
APEX_JSON.write('NAME',nodes.name);
V_STATUS := getnodestatus(nodes.name); --This can throw an exception
APEX_JSON.write('STATUS',V_STATUS);
APEX_JSON.CLOSE_OBJECT;
END LOOP;
APEX_JSON.CLOSE_ARRAY;
APEX_JSON.CLOSE_OBJECT;
EXCEPTION WHEN OTHERS THEN
--IF I get any error, then I have to write a different json, so I have to clear the json written till now, how to do it?
APEX_JSON.OPEN_OBJECT;
APEX_JSON.write('ERROR CODE',SQLCODE);
APEX_JSON.write('ERROR MSG',SQLERRM);
APEX_JSON.CLOSE_OBJECT;
END;
Run Code Online (Sandbox Code Playgroud)
PS:上面的代码只是一个例子。我在循环中编写的 JSON 对象要复杂得多,具有多级数组。
小智 5
APEX_JSON 可以写入临时 CLOB,而不是通过网关推送输出,如此处所述。
SET SERVEROUTPUT ON
DECLARE
l_cursor SYS_REFCURSOR;
BEGIN
OPEN l_cursor FOR
SELECT e.empno AS "employee_number",
e.ename AS "employee_name",
e.deptno AS "department_number"
FROM emp e
WHERE rownum <= 2;
APEX_JSON.initialize_clob_output;
APEX_JSON.open_object;
APEX_JSON.write('employees', l_cursor);
APEX_JSON.close_object;
DBMS_OUTPUT.put_line(APEX_JSON.get_clob_output);
APEX_JSON.free_output;
END;
/
Run Code Online (Sandbox Code Playgroud)
如此处所述:https : //oracle-base.com/articles/misc/apex_json-package-generate-and-parse-json-documents-in-oracle#temporary-clob
将您的 JSON 写入 CLOB。如果可行,请使用 HTP.print 将其推送到网关。如果失败,请发送您的错误消息。
干杯
蒂姆...