在经过一系列的波折后,还是觉得delphi读取并修改json文件来的方便;
在网络上找到一个delphi的三方库isuperobject,添加到项目后直接引用就行;
isuperobject中几个常用的函数:
演示代码:
//读取json文件 procedure tform2.button1click(sender: tobject); var rtext: textfile; tmp: utf8string; begin assignfile(rtext, 'c:\users\admin\desktop\f.txt'); reset(rtext); while not eof(rtext) do begin readln(rtext, tmp); res := res + tmp; end; closefile(rtext); memo1.lines.add(res); application.messagebox('加载成功!', '提示', mb_ok); end;
//处理json字符串,按照固定结构读取 procedure tform2.button2click(sender: tobject); var jret, jusers: isuperobject; aryusers: tsuperarray; gridstarttime, gridendtime, resultstarttime, resultendtime, windspeedborder, winddirectborder: string; po, pof, poh, posp: tpoint; num, count, i, j: integer; aryers, arrline, arrbar, arrspeed: tsuperarray; tlist: tstringlist; begin jret:=tsuperobject.create(stobject); tlist := tstringlist.create; tlist.delimiter := ','; jret := so(res); gridstarttime := jret.o['gridstarttime'].asstring(); memo1.lines.add(gridstarttime); gridendtime := jret.o['gridendtime'].asstring(); memo1.lines.add(gridendtime); resultstarttime := jret.o['resultstarttime'].asstring(); memo1.lines.add(resultstarttime); resultendtime := jret.o['resultendtime'].asstring(); memo1.lines.add(resultendtime); windspeedborder := jret.o['windspeedborder'].asstring(); memo1.lines.add(windspeedborder); winddirectborder := jret.o['winddirectborder'].asstring(); memo1.lines.add(winddirectborder); aryers := jret.o['winddirectline'].o['coordinates'].asarray(); count := aryers.length; for i := 0 to count - 1 do begin arrline := aryers[i].o['mwinddirectbaseline'].asarray(); for j := 0 to arrline.length - 1 do begin // tlist.delimitedtext := arrline[j].o['p'].asstring(); // po:=point(strtoint(tlist[0]),strtoint(tlist[1])); memo1.lines.add(arrline[j].o['p'].asstring()); end; arrbar := aryers[i].o['mwinddirectbars'].asarray(); for j := 0 to arrbar.length - 1 do begin // tlist.delimitedtext := arrbar[j].o['f'].o['p'].asstring(); // pof:=point(strtoint(tlist[0]),strtoint(tlist[1])); // tlist.delimitedtext := arrbar[j].o['h'].o['p'].asstring(); // poh:=point(strtoint(tlist[0]),strtoint(tlist[1])); memo1.lines.add('f: ' + arrbar[j].o['f'].o['p'].asstring() + ' h: ' + arrbar[j].o['h'].o['p'].asstring()); end; end; aryers := jret.o['windspeedline'].o['coordinates'].asarray(); count := aryers.length; for i := 0 to count - 1 do begin arrspeed := aryers[i].asarray(); for j := 0 to arrspeed.length - 1 do begin // tlist.delimitedtext := arrspeed[j].o['p'].asstring(); // posp:=point(strtoint(tlist[0]),strtoint(tlist[1])); memo1.lines.add(arrspeed[j].o['p'].asstring()); end; end; application.messagebox('处理完成!', '提示', mb_ok); end;
//修改json文件并保存 procedure tform2.button3click(sender: tobject); var jfields, jitems, jo, jret: isuperobject; date: string; num, count, i, j: integer; txt:textfile; aryers, arrline, arrbar, arrspeed: tsuperarray; begin jret := so(res); if jret = nil then application.messagebox('读取失败!', '提示', mb_ok); // date := formatdatetime('yyyy-mm-dd hh:nn:ss', now()); jret.s['gridstarttime'] := date; jret.s['gridendtime'] := date; jret.s['resultstarttime'] := date; jret.s['resultendtime'] := date; jret.s['windspeedborder'] := date; jret.s['winddirectborder'] := date; jret.i['gridmethod']:=2; count:=jret.o['winddirectline'].a['coordinates'].length; for i := 0 to count - 1 do begin num:= jret.o['winddirectline'].a['coordinates'][i].a['mwinddirectbaseline'].length; for j := 0 to num - 1 do begin jret.o['winddirectline'].a['coordinates'][i].a['mwinddirectbaseline'][j].s['p']:=inttostr(i)+' ,'+inttostr(j); end; end; assignfile(txt,'e:\p.gal'); //指定文件路径 rewrite(txt); //创建并打开一个新文件(或覆盖原有文件) writeln(txt,jret.asstring()); closefile(txt); //关闭打开的文件 application.messagebox(pchar(jret.o['gridstarttime'].asstring()), '提示', mb_ok); end;
注意:
如对本文有疑问, 点击进行留言回复!!
uniGUI之自定义JS事件动作ClientEvents(30)
TChart-数据编辑(TChartListBox,TeeCommander)
网友评论