delphi解析json資料的方法
阿新 • • 發佈:2019-01-26
function TFrm_Main.getquery: string;
var
vjson:ISuperObject; //Vitem
s1,s2:string;
PathStr:string;
ch,ph:string;
i,j:integer;
mylist:TStringlist;
b,a:integer;
sList: TStrings;
aSuperArray: TSuperArray;
begin
if fileexists('E:\servercs\openbrake_machine.txt') then
begin
mylist:=TStringlist.Create;
mylist.LoadFromFile('E:\servercs\openbrake_machine.txt');{讀檔案到mylist}
end;
a:=mylist.Count;{獲得mylist的count}
s1:=getqueryclass;
vJson:=so(s1);
if vJson['ResultNum'].AsInteger=0 then
exit;
asuperarray:=vjson['Result'].asarray;
for j:=0 to asuperarray.Length-1 do
BEGIN
if ((hourof(strtodatetime(asuperarray[j]['DrvDateTime'].AsString))*60-hourof(strtodatetime(FormatDateTime('hh:nn:ss',Now())))*60)+(minuteof(strtodatetime(asuperarray[j]['DrvDateTime'].AsString))-minuteof(strtodatetime(FormatDateTime('hh:nn:ss',Now()))))<word(20)) and ((hourof(strtodatetime(asuperarray[j]['DrvDateTime'].AsString))*60-hourof(strtodatetime(FormatDateTime('hh:nn:ss',Now())))*60)+(minuteof(strtodatetime(asuperarray[j]['DrvDateTime'].AsString))-minuteof(strtodatetime(FormatDateTime('hh:nn:ss',Now()))))>word(0)) then
begin
if length(asuperarray[j]['BusNumber'].AsString)>3 then
begin
for b:=0 to a-1 do
begin
// PathStr:=ExtractFilePath(Application.ExeName)+'openbrake_machine.txt' ;
s:=mylist.Strings[b];
i:=pos(',',s);
ph:=copy(s,0,i-1);
ch:=copy(s,i+1,length(s)-1);
if asuperarray[j]['BusNumber'].AsString=ch then
begin
deletefile(ExtractFilePath(Application.ExeName)+'資料\'+ph+'.txt');
s2:=ph+'號站臺的 '+asuperarray[j]['BusNumber'].AsString+' 車輛於'+asuperarray[j]['DrvDateTime'].AsString+'發往'+asuperarray[j]['StopName'].AsString; //發車時間
sList := TStringList.Create;
PathStr:=ExtractFilePath(Application.ExeName)+'資料\'+ph+'.txt' ;
try
if not DirectoryExists(ExtractFilePath(PathStr)) then
begin
ForceDirectories(ExtractFilePath(PathStr));
end;
if FileExists(PathStr) then
begin
sList.LoadFromFile(PathStr);
end;
sList.Add(s2);//#13#10
sList.SaveToFile(PathStr);
finally
sList.Free;
end;
end;
end;
end;
end;
end;
end;
var
vjson:ISuperObject; //Vitem
s1,s2:string;
PathStr:string;
ch,ph:string;
i,j:integer;
mylist:TStringlist;
b,a:integer;
sList: TStrings;
aSuperArray: TSuperArray;
begin
if fileexists('E:\servercs\openbrake_machine.txt') then
begin
mylist:=TStringlist.Create;
mylist.LoadFromFile('E:\servercs\openbrake_machine.txt');{讀檔案到mylist}
end;
a:=mylist.Count;{獲得mylist的count}
s1:=getqueryclass;
vJson:=so(s1);
if vJson['ResultNum'].AsInteger=0 then
exit;
asuperarray:=vjson['Result'].asarray;
for j:=0 to asuperarray.Length-1 do
BEGIN
if ((hourof(strtodatetime(asuperarray[j]['DrvDateTime'].AsString))*60-hourof(strtodatetime(FormatDateTime('hh:nn:ss',Now())))*60)+(minuteof(strtodatetime(asuperarray[j]['DrvDateTime'].AsString))-minuteof(strtodatetime(FormatDateTime('hh:nn:ss',Now()))))<word(20)) and ((hourof(strtodatetime(asuperarray[j]['DrvDateTime'].AsString))*60-hourof(strtodatetime(FormatDateTime('hh:nn:ss',Now())))*60)+(minuteof(strtodatetime(asuperarray[j]['DrvDateTime'].AsString))-minuteof(strtodatetime(FormatDateTime('hh:nn:ss',Now()))))>word(0)) then
begin
if length(asuperarray[j]['BusNumber'].AsString)>3 then
begin
for b:=0 to a-1 do
begin
// PathStr:=ExtractFilePath(Application.ExeName)+'openbrake_machine.txt' ;
s:=mylist.Strings[b];
i:=pos(',',s);
ph:=copy(s,0,i-1);
ch:=copy(s,i+1,length(s)-1);
if asuperarray[j]['BusNumber'].AsString=ch then
begin
deletefile(ExtractFilePath(Application.ExeName)+'資料\'+ph+'.txt');
s2:=ph+'號站臺的 '+asuperarray[j]['BusNumber'].AsString+' 車輛於'+asuperarray[j]['DrvDateTime'].AsString+'發往'+asuperarray[j]['StopName'].AsString; //發車時間
sList := TStringList.Create;
PathStr:=ExtractFilePath(Application.ExeName)+'資料\'+ph+'.txt' ;
try
if not DirectoryExists(ExtractFilePath(PathStr)) then
begin
ForceDirectories(ExtractFilePath(PathStr));
end;
if FileExists(PathStr) then
begin
sList.LoadFromFile(PathStr);
end;
sList.Add(s2);//#13#10
sList.SaveToFile(PathStr);
finally
sList.Free;
end;
end;
end;
end;
end;
end;
end;