在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在经过一系列的波折后,还是觉得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; 注意:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论