在PL SQL中使用APEX_JSON解析JSON

时间:2016-09-01 17:40:10

标签: oracle plsql oracle-apex oracle-apex-5

我正在尝试使用APEX_JSON解析PL / SQL中的JSON

我的JSON回复就像..

[
  {
    "NAME": "SAMPLE123",
    "SECOND_NAME": "MIDWEST MEDIA GROUP, INC",
    "ADDRESS": null,
    "PHONE_NUM": "050149603"
  },
  {
    "PHONE_NUM": "010609568",
    "ADDRESS": "BETTER VENDORS ASSOCIATION",
    "SECOND_NAME": "B V A CO OP INC",
    "NAME": "SAMPLE123"
  },
  {
    "PHONE_NUM": "111942970",
    "ADDRESS": null,
    "NAME": "SAMPLE123",
    "SECOND_NAME": "BALDWIN'S BUSINESS SYSTEMS, INC."
  },
  {
    "SECOND_NAME": "INNOVATIVE SALES TECHNOLO",
    "NAME": "SAMPLE123",
    "ADDRESS": null,
    "PHONE_NUM": "626904713"
  },
  {
    "ADDRESS": null,
    "PHONE_NUM": "050717132",
    "SECOND_NAME": "JEFFREY A. AVNY ATTORNEY AT LAW",
    "NAME": "SAMPLE123"
  },
  {
    "PHONE_NUM": "079203229",
    "ADDRESS": null,
    "SECOND_NAME": "3CLOUDS INC",
    "NAME": "SAMPLE123"
  },
  {
    "SECOND_NAME": "ARTHUR N SKLADMAN MD SC",
    "NAME": "SAMPLE123",
    "PHONE_NUM": "792034886",
    "ADDRESS": "SKLADMAN, ARTHUR"
  }]

我在PL / SQL

中编写了以下代码
sJsonIndex         APEX_JSON.t_values;

APEX_JSON.parse(sJsonIndex, clob_buff);

DBMS_OUTPUT.PUT_LINE(clob_buff);

sCount := APEX_JSON.get_count(p_path => '.' , p_values => sJsonIndex);

DBMS_OUTPUT.PUT_LINE('sCount ' || sCount);

IF sCount > 0 THEN

    FOR i in 1 .. sCount LOOP

        A_id := APEX_JSON.get_varchar2(
                            p_values => sJsonIndex, 
                            p_path => 'NAME['|| i ||']');

        --A_id := APEX_JSON.get_varchar2('NAME['|| i ||']');

        DBMS_OUTPUT.PUT_LINE('sCount i ' || i);

        DBMS_OUTPUT.PUT_LINE('A_id i ' || A_id);                   


    END LOOP;

END IF;

我得到以下输出:

sCount i 1
A_id i 
sCount i 2
A_id i 
sCount i 3
A_id i 
sCount i 4
A_id i 
sCount i 5
A_id i 
sCount i 6
A_id i 
sCount i 7
A_id i 
sCount i 8
A_id i 
sCount i 9
A_id i 
sCount i 10
A_id i 

有人可以帮忙吗?..我是APEX_JSON的新手

2 个答案:

答案 0 :(得分:3)

你关闭了 - 你的json包含一系列记录,而且这个数组是未命名的:

APEX_JSON.get_varchar2(p_path => '['|| i ||'].NAME', p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '['|| i ||'].SECOND_NAME', p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '['|| i ||'].ADDRESS', p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '['|| i ||'].PHONE_NUM', p_values => sJsonIndex);

答案 1 :(得分:1)

稍微调整一下Jeffrey Kemp回答:

APEX_JSON.get_varchar2(p_path => '[%d].NAME', p0 => i, p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '[%d].SECOND_NAME', p0 => i, p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '[%d].ADDRESS', p0 => i, p_values => sJsonIndex);
APEX_JSON.get_varchar2(p_path => '[%d].PHONE_NUM', p0 => i, p_values => sJsonIndex);