2017-08-04 67 views
0

我这里这段代码...提取整个JSON元素的子元素

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包含沿着“{‘东西’东西线:{‘猫’: “喵”, “狗”: “纬”}”。然而它什么都没有返回。

回答

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

有一个Oracle函数将返回整个字符串?我一直无法在网上找到关于它的任何内容。 – derpyderp

+0

更新为将'content'作为字符串返回。 –