如何解析存储在数组中的Oracle APEX v5.1中的JSON对象

时间:2017-06-21 08:30:17

标签: javascript json oracle-apex

我有以下示例JSON Object数组,即:

[
{
"type": "Campaign",
"currentStatus": "Active",
"id": "206",
"depth": "complete",
"folderId": "1428",
"name": "Car Loan",
"isReadOnly": "false"
},
{
"type": "Debate",
"currentStatus": "Active",
"id": "207",
"depth": "pending",
"folderId": "1429",
"name": "House Loan",
"isReadOnly": "true"
}
]

基于以上所述,我不确定如何在这个数组中迭代这些JSON对象?

对于每个对象,我需要生成一个报告,显示这两个JSON对象记录的所有列。

我查看过apex_json.get_count但不确定如何应用此数组。

1 个答案:

答案 0 :(得分:0)

如果您使用的是12c数据库,可以使用json_table: (据我所知,你需要一个父标签。在我使用的例子中," row")

SELECT jsn.*
  FROM   JSON_TABLE('{"row":[{"type": "Campaign","currentStatus": "Active","id": "206","depth": "complete","folderId": "1428","name": "Car Loan","isReadOnly": "false"},{"type": "Debate","currentStatus": "Active","id": "207","depth": "pending","folderId": "1429","name": "House Loan","isReadOnly": "true"}] }'
             , '$.row[*]'
     COLUMNS (type          VARCHAR2(50 CHAR) PATH '$.type',
              currentStatus VARCHAR2(50 CHAR) PATH '$.currentStatus',
              id            VARCHAR2(50 CHAR) PATH '$.id',
              depth         VARCHAR2(50 CHAR) PATH '$.depth',
              folderId      VARCHAR2(50 CHAR) PATH '$.folderId',
              name          VARCHAR2(50 CHAR) PATH '$.name',
              isReadOnly    VARCHAR2(50 CHAR) PATH '$.isReadOnly')) jsn; 

如果你使用12c数据库,你可以使用apex_json。为了在报告中使用它,管道功能可以是一个选项:

-- Create the types to support the table function.
DROP TYPE t_tf_tab;
DROP TYPE t_tf_row;

CREATE TYPE t_tf_row AS OBJECT (
  type          varchar2(50),
  currentStatus varchar2(50),
  id            varchar2(50),
  depth         varchar2(50),
  folderId      varchar2(50),
  name          varchar2(50),
  isReadOnly    varchar2(50)
);
/

CREATE TYPE t_tf_tab IS TABLE OF t_tf_row;
/

-- Build the table function itself.
CREATE OR REPLACE FUNCTION get_tab_tf RETURN t_tf_tab AS
  l_tab  t_tf_tab := t_tf_tab();
  l_json CLOB := '{"row": [ {
                                "type": "Campaign",
                                "currentStatus": "Active",
                                "id": "206",
                                "depth": "complete",
                                "folderId": "1428",
                                "name": "Car Loan",
                                "isReadOnly": "false"
                                },
                                {
                                "type": "Debate",
                                "currentStatus": "Active",
                                "id": "207",
                                "depth": "pending",
                                "folderId": "1429",
                                "name": "House Loan",
                                "isReadOnly": "true"
                                }
                                ] }';

     j apex_json.t_values;
     l_path  VARCHAR2(100);

    BEGIN
      apex_json.parse(l_json);

      FOR i IN 1..apex_json.get_count(p_path   => 'row')
      LOOP
        l_tab.extend;
        l_tab(l_tab.last) := t_tf_row( apex_json.get_varchar2( p_path => 'row[%d].type', p0 => i),
                                       apex_json.get_varchar2( p_path => 'row[%d].currentStatus', p0 => i),
                                       apex_json.get_varchar2( p_path => 'row[%d].id', p0 => i),
                                       apex_json.get_varchar2( p_path => 'row[%d].depth', p0 => i),
                                       apex_json.get_varchar2( p_path => 'row[%d].folderId', p0 => i),
                                       apex_json.get_varchar2( p_path => 'row[%d].name', p0 => i),
                                       apex_json.get_varchar2( p_path => 'row[%d].isReadOnly', p0 => i)
                                     );
      END LOOP;

  RETURN l_tab;
END;
/

-- Test it.
SELECT *
FROM   TABLE(get_tab_tf)
ORDER BY id DESC;