我在json中有一个数组,我想在oracle方法中写它。但是我在某个地方找不到我的错误。我尝试将其全部编写成一个循环,但代码可以编译但不编写。我要做的第一件事是使用循环查找第一个对象。然后,我在对象中间使用另一个循环来记录其中的数据!
v_clob := iot_general.blob_to_clob(p_blob);
apex_json.parse(tv, v_clob);
sCount := APEX_JSON.get_count(p_path => 'GroupSensor' , p_values => tv);
IF sCount > 0 THEN
FOR i in 1 .. sCount LOOP
v_id := apex_json.get_varchar2(p_path => 'GroupSensor.SerialNumber['|| i ||']', p_values => tv);
cCount := APEX_JSON.get_count(p_path => 'GroupSensor.GroupBob['|| i ||']' , p_values => tv);
IF cCount > 0 THEN
FOR q in 1 .. cCount LOOP
q_temp := apex_json.get_varchar2(p_path => 'GroupSensor.GroupBob['|| i ||']['|| q ||']', p_values => tv);
INSERT INTO SILO_SENSOR( NAME, DEVICES_ID)
VALUES (q_temp,v_id );
commit;
END LOOP;
END IF;
END LOOP;
END IF;
这是我的json
{
"GroupSensor": [
{
"silos": 1,
"GroupBob": [
"SENSOR0001",
"SENSOR0002",
"SENSOR0003",
"SENSOR0004",
"SENSOR0005",
"SENSOR0006",
"SENSOR0007",
"SENSOR0008",
"SENSOR0009",
"SENSOR0010"
],
"SerialNumber": "1701"
},
{
"silos": 1,
"GroupBob": [
"SENSOR0011",
"SENSOR0012",
"SENSOR0013",
"SENSOR0014",
"SENSOR0015",
"SENSOR0016",
"SENSOR0017",
"SENSOR0018",
"SENSOR0019"
],
"SerialNumber": "1702"
},
{
"silos": 1,
"GroupBob": [
"SENSOR0020",
"SENSOR0021",
"SENSOR0022",
"SENSOR0023",
"SENSOR0024",
"SENSOR0025",
"SENSOR0026",
"SENSOR0027",
"SENSOR0028"
],
"SerialNumber": "1703"
}
]
}
我想将数据写入SILO_SENSOR表,就这样
NAME DEVICES_ID
SENSOR0001 1701
SENSOR0002 1701
SENSOR0003 1701
SENSOR0004 1701
SENSOR0005 1701
SENSOR0006 1701
SENSOR0007 1701
SENSOR0008 1701
SENSOR0009 1701
SENSOR0010 1701
您没有正确访问数组:
尝试以下操作:
BEGIN
apex_json.parse(tv, v_clob);
scount := apex_json.get_count(p_path => 'GroupSensor', p_values => tv);
IF scount > 0 THEN
FOR i IN 1..scount LOOP
v_id := apex_json.get_varchar2('GroupSensor[%d].SerialNumber', i);
ccount := apex_json.get_count(p_path => 'GroupSensor[%d].GroupBob', p0 => i, p_values => tv);
IF ccount > 0 THEN
FOR q IN 1..ccount LOOP
q_temp := apex_json.get_varchar2('GroupSensor[%d].GroupBob[%d]', i, q);
INSERT INTO silo_sensor (
name,
devices_id
) VALUES (
q_temp,
v_id
);
COMMIT;
END LOOP;
END IF;
END LOOP;
END IF;
END;
我做了
v_clob := iot_general.blob_to_clob(p_blob);
apex_json.parse(tv, v_clob);
sCount := APEX_JSON.get_count(p_path => 'GroupSensor' , p_values => tv);
IF sCount > 0 THEN
FOR i in 1 .. sCount LOOP
v_id := apex_json.get_varchar2(p_path => 'GroupSensor['|| i ||'].SerialNumber', p_values => tv);
cCount := APEX_JSON.get_count(p_path => 'GroupSensor['|| i ||'].GroupBob' , p_values => tv);
IF cCount > 0 THEN
FOR q in 1 .. cCount LOOP
q_temp := apex_json.get_varchar2(p_path => 'GroupSensor['|| i ||'].GroupBob['|| q ||']', p_values => tv);
INSERT INTO SILO_SENSOR( NAME, DEVICES_ID)
VALUES (q_temp,v_id );
commit;
END LOOP;
END IF;
END LOOP;
END IF;