delphi7解析json数据
1. 定义一个(TsuperArray的变量) aSuperArray: TSuperArray;(需要引入Ticketservice)
2.定义vjson:ISuperObject;(引入Tsuperobject)
3.vJson:=so(s1);将json数据加载到vjson中
4.第一层key值直接获取 asuperarray:=vjson['Result'].asarray;
5。如果又第二层key值,先加载第一层key,在通过第一层value值获取第二层的值,如果第二层是数组,则用循环价值
for j:=0 to asuperarray.Length-1 do
begin
str:=asuperarray[j]['BusNumber'].AsString;
end;
//这里是将json数据解析出来后存到text文本中
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;