declare
l_json_doc VARCHAR2(32767);
l_numcols number;
l_numrows number;
begin
l_json_doc := '{
"table": {
"name": "sometablename",
"numofcolumns": 5,
"numofrows": 5,
"colheadings": [{
"colname": "customcol1",
"coltype": "number"
},
{
"colname": "customcol2",
"coltype": "varchar2"
},
{
"colname": "customcol3",
"coltype": "varchar2"
},
{
"colname": "customcol4",
"coltype": "varchar2"
},
{
"colname": "customcol5",
"coltype": "number"
}
],
"data": [{
"customcol1": "datacolumn1",
"customcol2": "datacolumn2",
"customcol3": "datacolumn3",
"customcol4": "datacolumn4",
"customcol5": "datacolumn5"
},
{
"customcol1": "2datacolumn1",
"customcol2": "2datacolumn2",
"customcol3": "2datacolumn3",
"customcol4": "2datacolumn4",
"customcol5": "2datacolumn5"
},
{
"customcol1": "3datacolumn1",
"customcol2": "3datacolumn2",
"customcol3": "3datacolumn3",
"customcol4": "3datacolumn4",
"customcol5": "3datacolumn5"
},
{
"customcol1": "4datacolumn1",
"customcol2": "4datacolumn2",
"customcol3": "4datacolumn3",
"customcol4": "4datacolumn4",
"customcol5": "4datacolumn5"
}
]
}
}';
APEX_JSON.parse(l_json_doc);
l_numcols := APEX_JSON.get_count(p_path => 'table.colheadings');
l_numrows := APEX_JSON.get_count(p_path => 'table.data');
FOR i IN 1 .. l_numrows LOOP
FOR j IN 1 .. l_numcols LOOP
dbms_output.put_line('TEST ' || APEX_JSON.get_varchar2(p_path => 'table.data[%d]') );
END LOOP;
END LOOP;
end;
这是我的代码,它应该提取数据数组对象。我期待以下输出:
TEST {"customcol1": "datacolumn1","customcol2": "datacolumn2","customcol3": "datacolumn3","customcol4": "datacolumn4","customcol5": "datacolumn5"}
TEST { "customcol1": "2datacolumn1","customcol2": "2datacolumn2","customcol3": "2datacolumn3","customcol4": "2datacolumn4","customcol5": "2datacolumn5"}
等等......
但是当我尝试使用APEX_JSON.get_varchar2从数据数组中获取json对象时,它返回空
答案 0 :(得分:0)
那里的代码存在两个问题
get_varchar2
功能提供索引。所以路径不完整。您需要在该函数的p0
参数中提供值APEX_JSON.get_varchar2
的工作方式。你期待着
函数返回数据数组中的整个JSON对象,但get_varchar2
不能这样做。它只能获得指定路径上的VARCHAR VALUE 。它无法让你获得整个物体。输出
DECLARE
l_json_doc VARCHAR2 (32767);
l_numcols NUMBER;
l_numrows NUMBER;
v_colname VARCHAR2 (32767);
BEGIN
l_json_doc := '{
"table": {
"name": "sometablename",
"numofcolumns": 5,
"numofrows": 5,
"colheadings": [{
"colname": "customcol1",
"coltype": "number"
},
{
"colname": "customcol2",
"coltype": "varchar2"
},
{
"colname": "customcol3",
"coltype": "varchar2"
},
{
"colname": "customcol4",
"coltype": "varchar2"
},
{
"colname": "customcol5",
"coltype": "number"
}
],
"data": [{
"customcol1": "datacolumn1",
"customcol2": "datacolumn2",
"customcol3": "datacolumn3",
"customcol4": "datacolumn4",
"customcol5": "datacolumn5"
},
{
"customcol1": "2datacolumn1",
"customcol2": "2datacolumn2",
"customcol3": "2datacolumn3",
"customcol4": "2datacolumn4",
"customcol5": "2datacolumn5"
},
{
"customcol1": "3datacolumn1",
"customcol2": "3datacolumn2",
"customcol3": "3datacolumn3",
"customcol4": "3datacolumn4",
"customcol5": "3datacolumn5"
},
{
"customcol1": "4datacolumn1",
"customcol2": "4datacolumn2",
"customcol3": "4datacolumn3",
"customcol4": "4datacolumn4",
"customcol5": "4datacolumn5"
}
]
}
}';
APEX_JSON.parse (l_json_doc);
l_numcols := APEX_JSON.get_count (p_path => 'table.colheadings');
l_numrows := APEX_JSON.get_count (p_path => 'table.data');
FOR i IN 1 .. l_numrows
LOOP
DBMS_OUTPUT.put ('TEST {');
FOR j IN 1 .. l_numcols
LOOP
v_colname :=
apex_json.get_varchar2 ('table.colheadings[%d].colname', j);
DBMS_OUTPUT.put (
'"' || v_colname || '":"'
|| APEX_JSON.get_varchar2 (
p_path => 'table.data[%d].' || v_colname,
p0 => i)
|| '",');
END LOOP;
DBMS_OUTPUT.put_line ('}');
END LOOP;
END;
以下是代码的输出:
TEST {"customcol1":"datacolumn1","customcol2":"datacolumn2","customcol3":"datacolumn3","customcol4":"datacolumn4","customcol5":"datacolumn5",}
TEST {"customcol1":"2datacolumn1","customcol2":"2datacolumn2","customcol3":"2datacolumn3","customcol4":"2datacolumn4","customcol5":"2datacolumn5",}
TEST {"customcol1":"3datacolumn1","customcol2":"3datacolumn2","customcol3":"3datacolumn3","customcol4":"3datacolumn4","customcol5":"3datacolumn5",}
TEST {"customcol1":"4datacolumn1","customcol2":"4datacolumn2","customcol3":"4datacolumn3","customcol4":"4datacolumn4","customcol5":"4datacolumn5",}
注意:我不想删除每行中最后一个键:值对之后的最后一个逗号。如果你想要那么你必须将所有键值对存储在一个变量中,并RTRIM
逗号。