PL / SQL中的APEX_JSON.get_varchar2

时间:2016-10-03 20:16:21

标签: json oracle plsql oracle-apex-5

我试图从“response.AAPL.results.year_high.data”中提取值,因为有两个值。我找到了很多例子,但是数据中的值括号都是由标题单独标识,而我的不是。有人知道如何访问这些信息吗?

Oracle 12中的

提前谢谢。

     create or replace procedure test(p_where in varchar2, p_radius in number, p_room in number)
    is

    begin
      DECLARE
    l_param_list     VARCHAR2(512);
    l_http_request   UTL_HTTP.req;
    l_http_response  UTL_HTTP.resp;
    l_response_text  VARCHAR2(32767);
    l_members  WWV_FLOW_T_VARCHAR2;
    l_count     PLS_INTEGER;

    l_list json_list;
    obj json := json();
    l_json_values apex_json.t_values;
    arr json_list := json_list();

    l_paths apex_t_varchar2;

    BEGIN


     l_response_text := '{"response": {"AAPL": {"meta": {"status": "ok"}, "results": {"year_high": {"meta": {"status": "ok"}, "data": [["2016-09-30", 123.8200], ["2016-09-29", 125.0000]]}}}, "MSFT": {"meta": {"status": "ok"}, "results": {"year_high": {"meta": {"status": "ok"}, "data": ["2016-09-30", 58.7000]}}}}}';

     /*  DBMS_OUTPUT.put_line(l_response_text);*/
      apex_json.parse (l_response_text);
    /*  dbms_output.put_line (apex_json.get_varchar2(p_path => 'count')); */
    /*  dbms_output.put_line (apex_json.get_number (p_path   => 'response.MSFT.results.price.data',p0=>2,p_values =>l_json_values));*/

      DBMS_OUTPUT.put_line('----------------------------------------'); 
      DBMS_OUTPUT.put_line('Check elements (members) below a path');

      l_members := APEX_JSON.get_members(p_path=>'response.AAPL.results.year_high');
      DBMS_OUTPUT.put_line('Members Count     : ' || l_members.COUNT);

      FOR i IN 1 .. l_members.COUNT LOOP
        DBMS_OUTPUT.put_line('Member Item Idx   : ' || i); 
        DBMS_OUTPUT.put_line('Member Name       : ' || l_members(i)); 
      END LOOP;   




 /* This is were I would like to extract the value in the data member*/




      DBMS_OUTPUT.put_line('----------------------------------------'); 
      DBMS_OUTPUT.put_line('Employee Information (Loop through array)');

      l_count := APEX_JSON.get_count(p_path => 'response.AAPL.results.year_high.data');
      DBMS_OUTPUT.put_line('Employees Count   : ' || l_count);

      FOR i IN 1 .. l_count LOOP
        DBMS_OUTPUT.put_line('Employee Item Idx : ' || i); 

        DBMS_OUTPUT.put_line('Employee Number   : ' ||
          APEX_JSON.get_varchar2(p_path => 'response.AAPL.results.year_high.data[%d]', p0 => i)); 

        DBMS_OUTPUT.put_line('Employee Name     : ' ||
          APEX_JSON.get_varchar2(p_path => 'response.AAPL.results.year_high.data[%d]', p0 => i)); 
      END LOOP;                                  

    /*  dbms_output.put_line (apex_json.get_varchar2 ('response.MSFT.results.year_high.data[%d]', 1));
      dbms_output.put_line (apex_json.get_varchar2('response.MSFT.results.year_high.data[%d]', 2));
      dbms_output.put_line (apex_json.get_varchar2 ('response.AAPL.results.year_high.data[%d]',1));
      dbms_output.put_line (apex_json.get_varchar2('response.AAPL.results.year_high.data[%d]',2));
    */

    end;

    end test;

1 个答案:

答案 0 :(得分:2)

首先,拥有一组不同数据类型的行为非常奇怪。即:[[" 2016-09-30",123.8200],[" 2016-09-29",125.0000]]

通常你会有一个日期数组,数组,文本数组。没有混合。

正如您所看到的,数据是一个数组数组。所以你需要解决这个问题:

declare
   json   varchar2 (32767)
      := '{"response": {"AAPL": {"meta": {"status": "ok"}, "results": {"year_high": {"meta": {"status": "ok"}, "data": [["2016-09-30", 123.8200], ["2016-09-29", 125.0000]]}}}, "MSFT": {"meta": {"status": "ok"}, "results": {"year_high": {"meta": {"status": "ok"}, "data": ["2016-09-30", 58.7000]}}}}}';
begin
   apex_json.parse (json);

   dbms_output.put_line ('First value: ' || apex_json.get_varchar2 ('response.AAPL.results.year_high.data[1][1]'));
   dbms_output.put_line ('Second value: ' || apex_json.get_number ('response.AAPL.results.year_high.data[1][2]'));
end;

将输出:

First value: 2016-09-30
Second value: 123,82

编辑:

要使用循环,这就是在数组中混合类型的原因。谢天谢地,get_varchar2会将一个数字强制转换为文本而不会导致错误:

declare
   json   varchar2 (32767)
      := '{"response": {"AAPL": {"meta": {"status": "ok"}, "results": {"year_high": {"meta": {"status": "ok"}, "data": [["2016-09-30", 123.8200], ["2016-09-29", 125.0000]]}}}, "MSFT": {"meta": {"status": "ok"}, "results": {"year_high": {"meta": {"status": "ok"}, "data": ["2016-09-30", 58.7000]}}}}}';
begin
   apex_json.parse (json);

   for x in 1 .. nvl (apex_json.get_count ('response.AAPL.results.year_high.data'), -1) loop
      for y in 1 .. nvl (apex_json.get_count ('response.AAPL.results.year_high.data[%d]', x), -1) loop
         dbms_output.put_line ('First value: ' || apex_json.get_varchar2 ('response.AAPL.results.year_high.data[%d][%d]', x, y));
      end loop;
   end loop;
end;

输出:

First value: 2016-09-30
First value: 123.82
First value: 2016-09-29
First value: 125