當前位置:編程學習大全網 - 源碼下載 - 請教,有用pb解析JSON的方法例子麽

請教,有用pb解析JSON的方法例子麽

我用PB寫過壹個函數,把json串轉成datawindow.

不過只適用於單層次的json,就是描述壹個關系表的json.

global type f_json2datawindow from function_object

end type

forward prototypes

global function string f_json2datawindow (string json_file, ref datawindow dw_1)

end prototypes

global function string f_json2datawindow (string json_file, ref datawindow dw_1);string ls_file

ls_file=json_file

//查找替換全部的回車,避免影響判斷

ls_file=f_replace_string_quotecharacter(ls_file,char(13),' ')

ls_file=f_replace_string_quotecharacter(ls_file,char(10),' ')

//查找替換全部的TAB

ls_file=f_replace_string_quotecharacter(ls_file,char(9),' ')

//濾掉首尾空格

ls_file=trim(ls_file)

if len(ls_file) =0 then

return('!!無數據')

end if

if left(ls_file,1) <>'{' or right(ls_file,1)<>'}' then

return('!!請用花括號把全部數據括起來。')

end if

ls_file=mid(ls_file,2,len(ls_file) - 2) //取得花括號裏的部分

//取標題

string ls_title

string ls_parm[],ls_clm[]

string ls_clmname[]

long n,m,p,q,i

dw_1.reset()

n=f_gettagparm(ls_file,'"',ls_parm[])

if n<=2 then

return('!!請用半角雙引號把表單的標題欄括起來')

end if

ls_title=ls_parm[2]

//找之後的冒號

//得先找標題欄結尾 q

p=pos(ls_file,'"'+ls_parm[2]+'"')

q=p+len('"'+ls_parm[2]+'"') - 1

//去掉標題欄

ls_file=right(ls_file,len(ls_file) - q )

//過濾掉空格(標題欄與冒號之間的空格)

ls_file=trim(ls_file)

if left(ls_file,1)<>':' then

return('!!標題欄後應跟著冒號')

end if

//去掉冒號再濾空格再去掉首尾方括號

ls_file=trim(right(ls_file,len(ls_file) - 1))

if left(ls_file,1)<>'[' and right(ls_file,1)<>']' then

return('!!請將數據用方括號括起來')

end if

ls_file=trim(mid(ls_file,2,len(ls_file) - 2))

//得到數據

string ls_data

ls_data = ls_file

//應至少有1行,否則無法獲得字段名

//第壹個應該是左花括號

if left(ls_file,1)<>'{' then

return('!!第壹行數據應用左花括號“{”開頭')

end if

//找第壹行的右花括號

p=f_pos_outof_quote(ls_file,'}',1)

if p>=2 then

else

return('!!第壹行數據缺少右花括號“}”做結尾')

end if

//去掉花括號,獲得第1行

ls_file=trim(mid(ls_file,2, p - 2))

//列分解

n=f_gettagparm_quotecharacter(ls_file,',',ls_clm)

if n=0 then

return('!!列數為0')

end if

//各列取得列名

string ls_clmnames

for i= 1 to n

ls_clm[i]=trim(ls_clm[i]) // 去掉收尾空格

m=f_gettagparm_quotecharacter(ls_clm[i],':',ref ls_parm)

if m=0 then

return('!!無法取得第1行第'+string(i)+'列的列名')

end if

ls_parm[1]=trim(ls_parm[1])

ls_clmname[i]=ls_parm[1]

if left(ls_clmname[i],1)='"' then ls_clmname[i] = right(ls_clmname[i],len(ls_clmname[i]) - 1)

if right(ls_clmname[i],1)='"' then ls_clmname[i] = left(ls_clmname[i],len(ls_clmname[i]) - 1)

if pos(ls_clmname[i],'{')>=1 or pos(ls_clmname[i],'}')>=1 or pos(ls_clmname[i],'[')>=1 or pos(ls_clmname[i],']')>=1 &

or pos(ls_clmname[i],',')>=1 or pos(ls_clmname[i],char(9))>=1 then

return('!!第1行第'+string(i)+'列的列名中含有非法的字符')

end if

ls_clmnames = ls_clmnames + '['+ls_clmname[i]+']'

next

//生成數據窗口

string sqlstr

sqlstr='select'

for i= 1 to n

sqlstr=sqlstr +' lpad(~' ~',1000) as "'+ls_clmname[i]+'" ,'

next

sqlstr=left(sqlstr,len(sqlstr) - 1) +' from dual '

if f_gendw(dw_1,sqlstr) <>0 then

return('!!生成數據窗口失敗')

end if

//從ls_data加載數據

long ll_begin ,ll_end

long quote_count

long c,r

long ret

string ls_row

string ls_value[]

ls_data=trim(ls_data) //過濾首尾空格後,首尾應都是花括號

if left(ls_data,1)<>'{' then

return('!!獲取數據時,第1行開頭不是花括號')

end if

dw_1.setredraw(false)

p=1

ll_begin=p

q=f_pos_outof_quote(ls_data,'}',p)

r=1

do while p>=1

if q>=1 then

quote_count=f_charcount(mid(ls_data ,ll_begin +1,q - ll_begin - 1),'"') //數花括號之間的雙引號數量

if mod(quote_count,2)=1 then //是奇數,找到的花括號是數據內容,則忽略

q=f_pos_outof_quote(ls_data,'}',q+1)

else

ls_row=trim(mid(ls_data,ll_begin+1,q - ll_begin - 1))

r++

c++

dw_1.insertrow(c)

//解析列

n=f_gettagparm_quotecharacter(ls_row,',',ls_clm)

if n=0 then

dw_1.setredraw(true)

return('!!第'+string(c)+'行的列數為0')

end if

//各列取得列名

for i= 1 to n

ls_clm[i]=trim(ls_clm[i]) // 去掉收尾空格

m=f_gettagparm_quotecharacter(ls_clm[i],':',ref ls_parm)

if m=0 then

dw_1.setredraw(true)

return('!!無法取得第'+string(c)+'行第'+string(i)+'列的列名')

end if

ls_parm[1]=trim(ls_parm[1])

ls_clmname[i]=ls_parm[1]

if left(ls_clmname[i],1)='"' then ls_clmname[i] = right(ls_clmname[i],len(ls_clmname[i]) - 1)

if right(ls_clmname[i],1)='"' then ls_clmname[i] = left(ls_clmname[i],len(ls_clmname[i]) - 1)

ls_value[i]=trim(ls_parm[2])

if left(ls_value[i],1)='"' then ls_value[i] = right(ls_value[i],len(ls_value[i]) - 1)

if right(ls_value[i],1)='"' then ls_value[i] = left(ls_value[i],len(ls_value[i]) - 1)

ret=pos(ls_clmnames,'['+ls_clmname[i]+']')

if ret=0 then

dw_1.setredraw(true)

return('!!第'+string(c)+'行第'+string(i)+'列寫入數據窗口時,列名('+ls_clmname[i]+')不存在於第1行')

end if

ret=dw_1.setitem(c,ls_clmname[i],ls_value[i])

if isnull(ret) then

dw_1.setredraw(true)

return('!!第'+string(c)+'行第'+string(i)+'列寫入數據窗口時,參數為空')

elseif ret<>1 then

dw_1.setredraw(true)

return('!!第'+string(c)+'行第'+string(i)+'列寫入數據窗口時,行號或列名('+ls_clmname[i]+')不存在')

end if

next

p=f_pos_outof_quote(ls_data,'{',q + 1)

ll_begin=p

q=f_pos_outof_quote(ls_data,'}',p)

end if

else

dw_1.setredraw(true)

return('!!獲取數據時,第'+string(r)+'行找不到右邊的花括號')

end if

loop

dw_1.setredraw(true)

return(ls_title)

end function

  • 上一篇:西藏有哪些旅遊景點?
  • 下一篇:在壹行上寫多條python語句用什麽分割
  • copyright 2024編程學習大全網