delphi TNetHTTPClient读取json并解析完整示例  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

delphi TNetHTTPClient读取json并解析完整示例


lqdmj8yp8ixa7eef4dbm.gif


uses System.Net.HttpClientComponent, System.Generics.Collections, JSON;


function GetBinanceHighValue( const xValues: array of string ): string;

const

  aUrl = 'https://www.binance.com/tr/trade/BNB_USDT';

var

  aClient : TNetHTTPClient;

  sJSON, sSearch   : String;

  aJSONData   : TJSONValue;

  aJSONValue  : TJSONValue;

  i : Integer;

begin

  Result := '';

  aClient := TNetHTTPClient.Create(nil);

  try

    aClient.UserAgent   := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'; // 'Dalvik/2.1.0 (Linux; U; Android 8.0.0; SM-N950F Build/R16NW)';

    aClient.Accept      := 'text/html';

    sJSON               := aClient.Get( aUrl ).ContentAsString;


  // Parse Section

  // ------------------------------------------------------------------------ //

    sSearch := '<textarea id="__APP_DATA" style="display: none">';

    if pos(sSearch, sJSON ) > 0 then

    begin

      System.Delete(sJSON, 1, Pos(sSearch, sJSON) + Length(sSearch)-1);

      sSearch := '"productMap":';

      if pos(sSearch, sJSON ) > 0 then begin

        System.Delete(sJSON, 1, Pos(sSearch, sJSON) + Length(sSearch)-1);

        sJSON := Copy(sJSON, 1, Pos(',"updateTime":', sJSON)-1);

      end;

    end;


  // -------------------------------------------------------------------------- //

  // JSON Parse section

  // -------------------------------------------------------------------------- //

    aJSONData := JSON.TJSONObject.ParseJSONValue( TEncoding.ASCII.GetBytes( Trim(sJSON) ), 0);

    try

      if aJSONData <> nil then

      begin

        for i := Low(xValues) to High(xValues) do begin

          if aJSONData.FindValue(xValues[i]) <> nil then

          begin

            aJSONValue := aJSONData.GetValue<TJSONObject>(xValues[i]);

            if aJSONValue.FindValue('high') <> nil then

            begin

              if Result = ''

                then Result := xValues[i] + ': ' + ((aJSONValue as TJSONObject).Get('high').JsonValue as TJSONString).Value

                else Result := Result + ', ' + xValues[i] + ': ' + ((aJSONValue as TJSONObject).Get('high').JsonValue as TJSONString).Value;

            end;

          end;

        end;

      end else begin

        sJSON := '';

        MessageDlg('Veri ekranına ulaşılamadı....', mtError, [mbOk], 0 );

      end;

    finally

      aJSONData.Free;

    end;

  finally

    FreeAndNil(aClient);

  end;

end;



代码:(全选)

procedure TForm1.Button1Click(Sender: TObject);

var

  Sonuclar : String;

begin

  Sonuclar := GetBinanceHighValue( [ 'ETHUSDT', 'BNBUSDT' ] );

  Showmessage( Sonuclar );

end;



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.098834037780762 seconds