我刚刚开始使用PL / JSON,我能够解析非数组数据,例如名字,例如:
json_ext.get_string (json (l_list.get (i)), 'firstName');
但是,我无法弄清楚如何解析地址等数组数据,例如:
street1 := json_ext.get_string (json (l_list.get (i)), 'addresses.street1');
不起作用。
如何正确解析数组数据,使用:
street1 := json_ext.get_string (json (l_list.get (i)), 'addresses.street1');
用作例子?
以下是JSON的一个示例:
[
{
"firstName": "Edward",
"middleName": "Wolfgang",
"lastName": "Munster",
"addresses": [
{
"city": "",
"addressType": "home",
"state": "CA",
"street1": "1313 Mockingbird Lane",
"street2": ""
},
{
"city": "",
"addressType": "business",
"state": "CA",
"street1": "123 Morgan Rd.",
"street2": ""
}
],
}
以下是我正在使用的代码示例:
BEGIN
SELECT json
INTO l_json
FROM json_table
WHERE id = 1;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
l_json := NULL;
END;
l_list := json_list (l_json);
FOR i IN 1 .. l_list.COUNT
LOOP
--These parse correctly
firstname := json_ext.get_string (json (l_list.get (i)), 'firstName');
middlename := json_ext.get_string (json (l_list.get (i)), 'middleName');
lastname := json_ext.get_string (json (l_list.get (i)), 'lastName');
--These do not parse at all
addresstype := json_ext.get_string (json (l_list.get (i)), 'addresses.addressType');
street1 := json_ext.get_string (json (l_list.get (i)), 'addresses.street1');
street2 := json_ext.get_string (json (l_list.get (i)), 'addresses.street2');
city := json_ext.get_string (json (l_list.get (i)), 'addresses.city');
state := json_ext.get_string (json (l_list.get (i)), 'addresses.state');
答案 0 :(得分:0)
这很简单。只需检索列表并迭代它:
@Service
答案 1 :(得分:0)
上面的例子并不适用于我。我设法让以下工作。
set @n := '';
set @rn := 0;
update t
set name = if(@n = name, concat(name, (@rn := @rn + 1)),
if(@n := name, if(@rn := 0, name, name), name)
)
order by name;
答案 2 :(得分:0)
检查此示例:
DECLARE
OBJ JSON := JSON( '{"pjDATOS_CITAS":[{"SDACLAVE":"32709","PENDIENTES_CITAR":"3","CANTIDAD":"1"},{"SDACLAVE":"32708","PENDIENTES_CITAR":"3","CANTIDAD":"0"}]}' );
LIST_VALUE JSON_LIST;
BEGIN
LIST_VALUE := JSON_LIST(OBJ.GET('pjDATOS_CITAS'));
FOR I IN 1 .. LIST_VALUE.COUNT LOOP
DBMS_OUTPUT.PUT_LINE( 'SDACLAVE->'||JSON_EXT.GET_STRING( JSON(LIST_VALUE.GET(I)) , 'SDACLAVE') );
DBMS_OUTPUT.PUT_LINE( 'PENDIENTES_CITAR->'||JSON_EXT.GET_STRING( JSON(LIST_VALUE.GET(I)) , 'PENDIENTES_CITAR') );
DBMS_OUTPUT.PUT_LINE( 'CANTIDAD->'||JSON_EXT.GET_STRING( JSON(LIST_VALUE.GET(I)) , 'CANTIDAD') );
DBMS_OUTPUT.PUT_LINE( '-------------------------------------------------' );
END LOOP;
END;
并且您需要修复JSON文件...使用JSON检查此示例:
DECLARE
OBJ JSON := JSON( '{"info_any": {"middleName": "Wolfgang","lastName": "Munster","addresses": [{"city": "","state": "CA","addressType": "home","street2": "","street1": "1313 Mockingbird Lane"},{"city": "","state": "CA","addressType": "business","street2": "","street1": "123 Morgan Rd."}],"firstName": "Edward"}}' );
LIST_VALUE JSON_LIST;
BEGIN
OBJ := JSON ( OBJ.GET('info_any') );
DBMS_OUTPUT.PUT_LINE( 'middleName->'||JSON_EXT.GET_STRING( OBJ , 'middleName') );
DBMS_OUTPUT.PUT_LINE( 'firstName->' ||JSON_EXT.GET_STRING( OBJ , 'firstName') );
DBMS_OUTPUT.PUT_LINE( 'lastName->' ||JSON_EXT.GET_STRING( OBJ , 'lastName') );
DBMS_OUTPUT.PUT_LINE( '-------------------------------------------------' );
LIST_VALUE := JSON_LIST(OBJ.GET('addresses'));
FOR i IN 1 .. LIST_VALUE.COUNT LOOP
DBMS_OUTPUT.PUT_LINE( 'city->'||JSON_EXT.GET_STRING( JSON(LIST_VALUE.GET(I)) , 'city') );
DBMS_OUTPUT.PUT_LINE( 'state->'||JSON_EXT.GET_STRING( JSON(LIST_VALUE.GET(I)) , 'state') );
DBMS_OUTPUT.PUT_LINE( 'addressType->'||JSON_EXT.GET_STRING( JSON(LIST_VALUE.GET(I)) , 'addressType') );
DBMS_OUTPUT.PUT_LINE( 'street2->'||JSON_EXT.GET_STRING( JSON(LIST_VALUE.GET(I)) , 'street2') );
DBMS_OUTPUT.PUT_LINE( 'street1->'||JSON_EXT.GET_STRING( JSON(LIST_VALUE.GET(I)) , 'street1') );
DBMS_OUTPUT.PUT_LINE( '-------------------------------------------------' );
END LOOP;
END;
结果:
middleName->Wolfgang
firstName->Edward
lastName->Munster
-------------------------------------------------
city->
state->CA
addressType->home
street2->
street1->1313 Mockingbird Lane
-------------------------------------------------
city->
state->CA
addressType->business
street2->
street1->123 Morgan Rd.
-------------------------------------------------