我有一個如下所示的程序。它接受一個 JSON 物件作為輸入。此物件型別中的元素之一是帶有鍵的陣列samplekey。在某些情況下,JSON 物件p_object不包含samplekey陣列。在這些情況下,程序遇到運行時例外。在 PL/SQL 中處理這種情況的最佳方法是什么?我應該檢查與關鍵元素的存在,samplekey在物件p_object第一?如果是,我該怎么做?有沒有更好的方法來處理這種情況?
procedure(p_object in json_object_t)
as
lja json_array_t;
begin
lja := p_object.get_array('samplekey');
end;
uj5u.com熱心網友回復:
您可以使用 的HAS功能JSON_OBJECT_T。這將回傳,TRUE或者FALSEJSON 是否具有您搜索的密鑰。下面是一個如何使用它的例子。
DECLARE
PROCEDURE test_json (p_object IN json_object_t)
IS
BEGIN
IF p_object.has ('samplekey')
THEN
DBMS_OUTPUT.put_line ('JSON has the key "samplekey"');
ELSE
DBMS_OUTPUT.put_line ('JSON does NOT have the key "samplekey"');
END IF;
END;
BEGIN
test_json (json_object_t ('{}'));
test_json (json_object_t ('{"samplekey":123}'));
test_json (json_object_t ('{"otherkey":"test"}'));
END;
/
--Output
JSON does NOT have the key "samplekey"
JSON has the key "samplekey"
JSON does NOT have the key "samplekey"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399912.html
