APEX_JSON.get_varchar2 在 PL/SQL
APEX_JSON.get_varchar2 in PL/SQL
我试图从 "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;
首先,拥有不同数据类型的数组是一种非常奇怪的行为。即:[["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
我试图从 "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;
首先,拥有不同数据类型的数组是一种非常奇怪的行为。即:[["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