After a series of twists and turns, it is still convenient for delphi to read and modify json files;
Find a delphi tripartite library ISuperObject on the network, add it to the project and reference it directly;
Several commonly used functions in ISuperObject:
- Function SO(const s: SOString = '{}'): ISuperObject; overload; this function passes in json data string and returns an ISuperObject object, which is generally the first function we use when parsing json, such as jboj: = so (jsonstr).
- property O[const path: SOString]: ISuperObject read GetO write PutO; default; for example: jobj.O ['username'], this function is called by an ISuperObject object, the string in square brackets is the field name in json, and an ISuperObject object is returned.
- property S[const path: SOString]: SOString read GetS write PutS; this function is called by an ISuperObject object. Unlike O ['username'], it returns A SoString, that is, A string, using STR: = jobj. S ['username']; similarly, there are several other similar functions, such as I ['age'] returns an integer, B ['isenable'] returns A Boolean Type A ['users'] returns an array of tsuperarrays
- The functions of AsString, AsBoolean, AsInteger,AsArray and ISuperObject are used to convert ISuperObject to the corresponding data type.
Demo code:
//read json file 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('Loading succeeded!', 'Tips', MB_OK); end;
//Handle JSON String, reading according to fixed structure 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('Processing complete!', 'Tips', MB_OK); end;
//modify JSON File and save 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('Read failed!', 'Tips', 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'); //specify the path to a file rewrite(txt); //Create and open a new file (or overwrite the old one) Writeln(txt,jRet.AsString()); Closefile(txt); //Close open file application.MessageBox(Pchar(jRet.O['gridStartTime'].AsString()), 'Tips', MB_OK); end;
Be careful:
- When reading the file, if the json file contains Chinese format, it must be ensured that the get field is of UTF8String type.
- jRet.O['WindSpeedLine'].A['coordinates'][0].AsArray().add(jo); / / you can add json nested with array in this way.
- Jo read or declared: = so(); cannot be cleared with clear(), otherwise add(jo) will become empty data.