APEX_JSON嵌套解析

通过apex_json函数解释复杂的json串,结合:body变量使用,可能完成复杂的api开发, 除了用apex_json还用json_table的方法,另外再写文章介绍,以下是具体的apex_json使用示例:

样本JSON

{

 "RAW": {

  "LEVEL_1": "2",

  "LEVEL_1_ARRAY": [{

    "LEVEL_2": {

     "LEVEL_2_A": "Ashish",

     "LEVEL_2_ARRAY": [{

       "LEVEL_3_A": "00",

       "LEVEL_3_B": "ABC"

      },

      {

       "LEVEL_3_A": "01",

       "LEVEL_3_B": "CAB"

      }

     ],

     "LEVEL_2_B": {

      "LEVEL_3_C": "",

      "LEVEL_3_D": ""

     },

     "LEVEL_2_C": {

      "LEVEL_3_C_1": "",

      "LEVEL_3_C_2": "A145543"

     }

    }

   },

   {

    "LEVEL_2": {

     "LEVEL_2_A": "Ashish",

     "LEVEL_2_ARRAY": [{

       "LEVEL_3_A": "00",

       "LEVEL_3_B": "ABC"

      },

      {

       "LEVEL_3_A": "01",

       "LEVEL_3_B": "CAB"

      }

     ],

     "LEVEL_2_B": {

      "LEVEL_3_C": "",

      "LEVEL_3_D": ""

     },

     "LEVEL_2_C": {

      "LEVEL_3_C_1": "",

      "LEVEL_3_C_2": "A145543"

     }

    }

   }

  ]

 }

}

...JSON

样本PLSQL块

DECLARE

    l_json_text   VARCHAR2 (32767);

    l_count       PLS_INTEGER;

    l_members     wwv_flow_t_varchar2;

    l_paths       apex_t_varchar2;

    l_exists      BOOLEAN;

    l_count_2     PLS_INTEGER;

BEGIN

    l_json_text := '{

 "RAW": {

  "LEVEL_1": "20",

  "LEVEL_1_ARRAY": [{

    "LEVEL_2": {

     "LEVEL_2_A": "Ashish",

     "LEVEL_2_ARRAY": [{

       "LEVEL_3_A": "00",

       "LEVEL_3_B": "ABC"

      },

      {

       "LEVEL_3_A": "01",

       "LEVEL_3_B": "CAB"

      }

     ],

     "LEVEL_2_B": {

      "LEVEL_3_C": "LEVEL_3_C",

      "LEVEL_3_D": "LEVEL_3_D"

     },

     "LEVEL_2_C": {

      "LEVEL_3_C_1": "LEVEL_3_C_1",

      "LEVEL_3_C_2": "LEVEL_3_C_2"

     }

    }

   },

   {

    "LEVEL_2": {

     "LEVEL_2_A": "Sahay",

     "LEVEL_2_ARRAY": [{

       "LEVEL_3_A": "00",

       "LEVEL_3_B": "ABC"

      },

      {

       "LEVEL_3_A": "01",

       "LEVEL_3_B": "CAB"

      }

     ],

     "LEVEL_2_B": {

      "LEVEL_3_C": "LEVEL_3_C khashas",

      "LEVEL_3_D": "LEVEL_3_D aksjkajsjkasjka"

     },

     "LEVEL_2_C": {

      "LEVEL_3_C_1": "LEVEL_3_C_1 king",

      "LEVEL_3_C_2": "A145543"

     }

    }

   }

  ]

 }

}'   ;

   apex_json.parse (l_json_text);

    DBMS_OUTPUT.put_line ('----------------------------------------');

    DBMS_OUTPUT.put_line ( 'LEVEL_1 : ' || apex_json.get_varchar2 (p_path => 'RAW.LEVEL_1'));

   l_count := apex_json.get_count (p_path => 'RAW.LEVEL_1_ARRAY');

    DBMS_OUTPUT.put_line ('RAW.LEVEL_1_ARRAY count: ' || l_count);
    FOR i IN 1 .. l_count

    LOOP

        DBMS_OUTPUT.put_line ('RAW.LEVEL_1_ARRAY  Item Idx : ' || i);

        DBMS_OUTPUT.put_line (

               'LEVEL_2_A   : '

            || apex_json.get_varchar2 (

                   p_path   => 'RAW.LEVEL_1_ARRAY[%d].LEVEL_2.LEVEL_2_A',

                   p0       => i));

        l_count_2 :=

            apex_json.get_count (

                p_path   => 'RAW.LEVEL_1_ARRAY[%d].LEVEL_2.LEVEL_2_ARRAY',

                p0       => i);

        DBMS_OUTPUT.put_line ('LEVEL_2_ARRAY count   : ' || l_count_2);

        FOR j IN 1 .. l_count_2

        LOOP

            DBMS_OUTPUT.put_line (

                   ' LEVEL_3_A: '

                || apex_json.get_varchar2 (

                       p_path   => 'RAW.LEVEL_1_ARRAY[%d].LEVEL_2.LEVEL_2_ARRAY[%d].LEVEL_3_A',

                       p0       => i,

                       p1       => j));



            DBMS_OUTPUT.put_line (

                   'LEVEL_3_B : '

                || apex_json.get_varchar2 (

                       p_path   => 'RAW.LEVEL_1_ARRAY[%d].LEVEL_2.LEVEL_2_ARRAY[%d].LEVEL_3_B',

                       p0       => i,

                       p1       => j));

        END LOOP;



        DBMS_OUTPUT.put_line (

               'LEVEL_3_C   : '

            || apex_json.get_varchar2 (

                   p_path   => 'RAW.LEVEL_1_ARRAY[%d].LEVEL_2.LEVEL_2_B.LEVEL_3_C',

                   p0       => i));



        DBMS_OUTPUT.put_line (

               'LEVEL_3_D   : '

            || apex_json.get_varchar2 (

                   p_path   => 'RAW.LEVEL_1_ARRAY[%d].LEVEL_2.LEVEL_2_B.LEVEL_3_D',

                   p0       => i));



        DBMS_OUTPUT.put_line (

               'LEVEL_3_C_1   : '

            || apex_json.get_varchar2 (

                   p_path   => 'RAW.LEVEL_1_ARRAY[%d].LEVEL_2.LEVEL_2_C.LEVEL_3_C_1',

                   p0       => i));



        DBMS_OUTPUT.put_line (

               'LEVEL_3_C_2   : '

            || apex_json.get_varchar2 (

                   p_path   => 'RAW.LEVEL_1_ARRAY[%d].LEVEL_2.LEVEL_2_C.LEVEL_3_C_2',

                   p0       => i));

    END LOOP;

END;

/

SQL

输出:

LEVEL_1 : 20

RAW.LEVEL_1_ARRAY count: 2

RAW.LEVEL_1_ARRAY  Item Idx : 1

LEVEL_2_A   : Ashish

LEVEL_2_ARRAY count   : 2

LEVEL_3_A: 00

LEVEL_3_B : ABC
LEVEL_3_A: 01
LEVEL_3_B : CAB
LEVEL_3_C   : LEVEL_3_C
LEVEL_3_D   : LEVEL_3_D
LEVEL_3_C_1   : LEVEL_3_C_1
LEVEL_3_C_2   : LEVEL_3_C_2
RAW.LEVEL_1_ARRAY  Item Idx : 2
LEVEL_2_A   : Ashish
LEVEL_2_ARRAY count   : 2
LEVEL_3_A: 00
LEVEL_3_B : ABC
LEVEL_3_A: 01
LEVEL_3_B : CAB
LEVEL_3_C   : LEVEL_3_C khashas
LEVEL_3_D   : LEVEL_3_D aksjkajsjkasjka
LEVEL_3_C_1   : LEVEL_3_C_1 king
LEVEL_3_C_2   : A145543

文本

© 版权声明
THE END
喜欢就支持一下吧
点赞1
分享