Json 运算符似乎不适用于变量

Json operator doesn't seem to work with variable

测试查询:

select * from testarray(1120,'{"oserial":
["10002695","10002692"],"nserial":[{"serial":"10002693","price":"180.00"},
{"serial":"10002697","price":"180.00"}]}');
CREATE OR REPLACE FUNCTION public.testarray(salesid int,items json) 
RETURNS int
LANGUAGE plpgsql
AS $$
declare
  resu text;
  resu2 text := 'TH';
  ssrow RECORD;
  oserlen int := 0;
  counter int := 0;
begin
  select json_array_length(items::json->'oserial') into oserlen;
  while counter < oserlen loop
    -- if I change 0 to counter, this function does not work
    select (items::json#>>'{oserial,0}') into resu; 
    select * into ssrow 
    from salesserial 
    where fk_salesid=salesid 
      and serialnum=resu::int;

    insert into stockloct(serialnum,fk_barcode,source,exflag) 
    values(ssrow.serialnum,ssrow.fk_barcode,ssrow.fk_salesid,true);

    counter := counter + 1;
  end loop; 
  select items::json#>'{nserial,0,serial}' into resu2;
  return oserlen;
end;
$$;

我正在测试 PostgreSQL 函数。

下面这一行似乎不起作用,谁能帮帮我。提前致谢。我正在使用 Postgresql 9.5 版。如果我将 json 字符串中的 0 更改为计数器变量,则 select 不会 return 值。它只适用于数字,不适用于变量。

select (items::json#>>'{oserial,0}') into resu; 
-- if i change 0 to counter variable, this select returns null i think as the insert below the line fails; 

#>> 的右侧参数是一个文本数组。替换

select (items::json#>>'{oserial,0}') into resu; 

select items::json#>>array['nserial',counter::text] into resu;