Delphi調用REST

Delphi調用REST很簡單,首先在界面上放上:json

RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
RESTResponse1: TRESTResponse;spa

而後簡單調用便可:code

RESTClient1.BaseURL:=edtURL.Text;
RESTRequest1.Execute;
memLog.Text:=RESTResponse1.Content;

還能夠對結果進行進一部處理,好比解析JSON:blog

procedure TfrmMain.btnGetClick(Sender: TObject);
var
  jo,jo2:TJSONObject;
  jv:TJSONValue;
  ja:TJSONArray;
  jp:TJSONPair;
  i:Integer;
begin
  RESTClient1.BaseURL:=edtURL.Text;
  RESTRequest1.Execute;
  memLog.Text:=RESTResponse1.Content;

  jo:=TJSONObject.Create;
  ja:=jo.ParseJSONValue(RESTResponse1.Content) as TJSONArray;
  for jv in ja do
   begin
      jo2:=jv as TJSONObject;
      for i:=0 to jo2.Count-1 do
      begin
        jp:=jo2.Pairs[i];
        memLog.Lines.Add(jp.JsonString.ToString+':'+jp.JsonValue.ToString);
      end;
   end;

end;

在這裏我使用的是Delphi自帶的JSON解析,注意引用單元system.json。class

相關文章
相關標籤/搜索