提取整个JSON元素子元素

时间:2017-08-04 02:05:38

标签: json oracle

我这里有一些代码......

set serveroutput on;
DECLARE
    v_response varchar(3000) := '{"content":{"stuff":{"cat":"meow","dog":"woof"}},"http_code":401,"response_code":"-1"}';
    v_content varchar(3000);
BEGIN
    select json_value(v_response, '$.content') into v_content from dual;
    dbms_output.put_line('v_content: ' || v_content);
END; 

我希望变量v_content包含“{”stuff“:{”cat“:”meow“,”dog“:”woof“}'的行。然而,它什么也没有返回。

1 个答案:

答案 0 :(得分:1)

JSON_VALUE在JSON数据中找到指定的标量JSON值,并将其作为SQL值返回。

select json_value('{"content":{"stuff":{"cat":"meow","dog":"woof"}},"http_code":401,"response_code":"-1"}', '$.content.stuff.cat') from dual

返回meow

试试这个:

DECLARE 
  je JSON_ELEMENT_T;
  jo JSON_OBJECT_T;
  content JSON_OBJECT_T;
  v_response varchar(3000) := '{"content":{"stuff":{"cat":"meow","dog":"woof"}},"http_code":401,"response_code":"-1"}';
BEGIN
  je := JSON_ELEMENT_T.parse(v_response);
  IF (je.is_Object) THEN
      jo := treat(je AS JSON_OBJECT_T);
      content := jo.get_Object('content');
  END IF;
  DBMS_OUTPUT.put_line(content.to_string);
END;