PLSQL ORACLE 11g获取所有对象

xmq68pz9  于 2022-11-03  发布在  Oracle
关注(0)|答案(1)|浏览(160)

我有这个json = '[ {“id”:11、“职称”:“1111”,},{“编号”:22、“职称”:“2222”,},{“编号”:33、“答案”:“3333”} ]'
I如何获取数组中位置2(或任意位置)的所有对象结果字符串:“{“身份证”:22,“头衔”:“2222”,}'
谢谢你!

fjaof16o

fjaof16o1#

您可以创建自己的函数,如下所示:

Function get_element(p_json VarChar2, p_which_one Number) Return VarChar2 IS
    my_str      VarChar2(255) := '';
    start_pos   Number :=  0;
    end_pos     Number :=  0;
Begin
    start_pos := InStr(p_json, '{', 1, p_which_one);
    end_pos := InStr(p_json, '}', 1, p_which_one) + 1;
    my_str := my_str || SubStr(p_json, start_pos, end_pos - start_pos);
    Return my_str;
End get_element;

得到你想要的元素。

SET SERVEROUTPUT ON
Declare
    json_str    VarChar2(512) := '[ { "id": 11, "title": "1111", }, { "id": 22, "title": "2222", }, { "id": 33, "answer": "3333" }, { "id": 44, "title": "4444", }, { "id": 55, "title": "5555", }, { "id": 66, "answer": "6666" }  ]';
    my_str    VarChar2(50) := '';
Begin
    my_str := get_element(json_str, 2);
    DBMS_OUTPUT.PUT_LINE(my_str);
End;
--
--  Result for p_which_one = 2 is
--  { "id": 22, "title": "2222", }
--
--  and for p_which_one = 4
--  { "id": 44, "title": "4444", }

...或者您可以循环它们...

SET SERVEROUTPUT ON
Declare
    json_str    VarChar2(512) := '[ { "id": 11, "title": "1111", }, { "id": 22, "title": "2222", }, { "id": 33, "answer": "3333" }, { "id": 44, "title": "4444", }, { "id": 55, "title": "5555", }, { "id": 66, "answer": "6666" }  ]';
    my_str      VarChar2(255) := '';
Begin
    For i in 1..100 Loop
        if instr(json_str, '{', 1, i) > 0 Then
            my_str := get_element(json_str, i);
            DBMS_OUTPUT.PUT_LINE(my_str);
        else
            goto end_it;
        end if;
    End Loop;
  <<end_it>>
    null;
End;
/*  R e s u l t :
anonymous block completed
{ "id": 11, "title": "1111", }
{ "id": 22, "title": "2222", }
{ "id": 33, "answer": "3333" }
{ "id": 44, "title": "4444", }
{ "id": 55, "title": "5555", }
{ "id": 66, "answer": "6666" }

* /

相关问题