delphi解析json数据的方法

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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值