2016-10-03 13 views
1

我想從「response.AAPL.results.year_high.data」中提取值,因爲有兩個值。我發現了很多例子,但數據中的值括號都是通過標題單獨標識的,而我的不是。有誰知道如何訪問這些信息?PL/SQL中的APEX_JSON.get_varchar2

in 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; 

回答

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 
+0

謝謝奧拉維爾。長話短說,結果是由一家公司提供的,所以我無法控制格式。這就是我一直在努力尋找訪問數據的方式的原因。我能夠從使用APEX製作的帖子中提取數據。有沒有辦法使用遊標(循環),使用我的價值? –

+0

至於格式,我認爲我沒有任何控制權,但我可能是錯的,因爲我是綠色的APEX。 –

+0

我已通過添加for循環示例來獲取數據來更新答案。 –