JSON 之 SuperObject(15): 實例 - 模擬 Google 搜索

本例測試效果圖:

web



代碼文件:數據庫


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses MsXML, SuperObject;

//這是將字符串轉換成 UTF 編碼的函數, 這裏用於 Google 的搜索地址
function ToUTF8Encode(str: string): string;
var
  b: Byte;
begin
  for b in BytesOf(UTF8Encode(str)) do
    Result := Format('%s%s%.2x', [Result, '%', b]);
end;

//這是給 ISuperObject 準備的方法
procedure Proc(const This, Params: ISuperObject; var Result: ISuperObject);
var
  jo: ISuperObject;
begin
  Form1.Memo1.Clear;
  for jo in Params['responseData.results'] do with Form1.Memo1.Lines do
  begin
    Add(jo.Format('%titleNoFormatting%:'));
    Add(jo.Format('%unescapedUrl%'));
    Add(EmptyStr);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  u = 'http://www.google.cn/uds/GwebSearch?callback=response&rsz=large&v=1.0&q=';
var
  jo: ISuperObject;
  req: IXMLHTTPRequest;
  url: WideString;
begin
  jo := SO;
  jo.M['response'] := @Proc; {搜索結果將是相似 response(...) 函數格式的字符串}

  url := u + ToUTF8Encode(Edit1.Text); {準備搜索地址}

  //搜索
  req := CoXMLHTTP.Create;
  req.open('Get', url, False, EmptyParam, EmptyParam);
  req.send(EmptyParam);

  //搜索結果在 req.responseText(後付其所有內容), 下面語句將調用上面的 Proc 過程.
  jo[req.responseText]; {這是在本系列"方法"一節用到的第二種調用方法}
end;

end.


窗體文件:編程


object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 358
  ClientWidth = 547
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 0
    Top = 0
    Width = 412
    Height = 358
    Align = alLeft
    Lines.Strings = (
      'Memo1')
    ScrollBars = ssBoth
    TabOrder = 0
    ExplicitHeight = 346
  end
  object Button1: TButton
    Left = 440
    Top = 72
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 418
    Top = 24
    Width = 121
    Height = 21
    TabOrder = 2
    Text = 'Delphi'
  end
end


下面是我以 "Delphi" 爲關鍵字搜索返回的 responseText, 能夠作個搜索工具了:c#

response(
{
 "responseStatus": 200,
 "responseData": {
  "cursor": {
   "pages": [
    {
     "label": 1,
     "start": "0"
    },{
     "label": 2,
     "start": "8"
    },{
     "label": 3,
     "start": "16"
    },{
     "label": 4,
     "start": "24"
    },{
     "label": 5,
     "start": "32"
    },{
     "label": 6,
     "start": "40"
    },{
     "label": 7,
     "start": "48"
    },{
     "label": 8,
     "start": "56"
    }],
   "currentPageIndex": 0,
   "estimatedResultCount": "6050000",
   "moreResultsUrl": "http:\/\/www.google.cn\/search?oe=utf8&ie=utf8&source=uds&start=0&hl=zh-CN&q=Delphi"
  },
  "results": [
   {
    "titleNoFormatting": "『Delphi園地』-源碼,控件,文檔,工具,免費下載,建設專業Delphi資源平臺",
    "url": "http:\/\/www.delphifans.com\/",
    "cacheUrl": "http:\/\/www.google.com\/search?q=cache:gxlSlu0OTqQJ:www.delphifans.com",
    "GsearchResultClass": "GwebSearch",
    "visibleUrl": "www.delphifans.com",
    "content": "提供Delhpi技巧文章、源碼、控件、文檔、工具等。",
    "title": "『<b>Delphi<\/b>園地』-源碼,控件,文檔,工具,免費下載,建設專業<b>Delphi<\/b>資源平臺",
    "unescapedUrl": "http:\/\/www.delphifans.com\/"
   },{
    "titleNoFormatting": "Delphi - 維基百科,自由的百科全書",
    "url": "http:\/\/zh.wikipedia.org\/wiki\/Delphi",
    "cacheUrl": "http:\/\/www.google.com\/search?q=cache:KPWnJZ_ZuuQJ:zh.wikipedia.org",
    "GsearchResultClass": "GwebSearch",
    "visibleUrl": "zh.wikipedia.org",
    "content": "<b>Delphi<\/b>,是Windows平臺下的著名快速應用程序開發工具(Rapid Application Development  ,簡稱RAD)。它的前身是在DOS下的產品BorlandTurbo Pascal,最先的版本由 <b>...<\/b>",
    "title": "<b>Delphi<\/b> - 維基百科,自由的百科全書",
    "unescapedUrl": "http:\/\/zh.wikipedia.org\/wiki\/Delphi"
   },{
    "titleNoFormatting": "DELPHI盒子- Delphi源代碼控件文檔工具下載",
    "url": "http:\/\/www.2ccc.com\/",
    "cacheUrl": "http:\/\/www.google.com\/search?q=cache:CF650RgvoJkJ:www.2ccc.com",
    "GsearchResultClass": "GwebSearch",
    "visibleUrl": "www.2ccc.com",
    "content": "<b>DELPHI<\/b>源代碼、組件、例子、演示及相關工具下載,文檔發佈,全站包含論壇,筆記等功能  ,天天更新。",
    "title": "<b>DELPHI<\/b>盒子- <b>Delphi<\/b>源代碼控件文檔工具下載",
    "unescapedUrl": "http:\/\/www.2ccc.com\/"
   },{
    "titleNoFormatting": "編程中國>> 技術教程>> 開發語言>> Delphi",
    "url": "http:\/\/www.bccn.net\/Article\/kfyy\/delphi\/",
    "cacheUrl": "http:\/\/www.google.com\/search?q=cache:vjiBcr2xV14J:www.bccn.net",
    "GsearchResultClass": "GwebSearch",
    "visibleUrl": "www.bccn.net",
    "content": "普通文章, 第二章<b>Delphi<\/b>面向對象的編程方法(一), 06-25. 普通文章 <b>...<\/b> 普通文章, 用  <b>Delphi<\/b>製做以瀏覽器爲界面的應用程序, 08-30. 普通文章, <b>Delphi<\/b> 中串口通信的實現 <b>...<\/b>",
    "title": "編程中國>> 技術教程>> 開發語言>> <b>Delphi<\/b>",
    "unescapedUrl": "http:\/\/www.bccn.net\/Article\/kfyy\/delphi\/"
   },{
    "titleNoFormatting": "Delphi is a leading global supplier for the automotive, computing ...",
    "url": "http:\/\/www.delphi.com\/",
    "cacheUrl": "http:\/\/www.google.com\/search?q=cache:r5kD35m3Ns0J:www.delphi.com",
    "GsearchResultClass": "GwebSearch",
    "visibleUrl": "www.delphi.com",
    "content": "Supplier of audio systems, modules, and components to manufacturers and   replacement parts to the after market worldwide.",
    "title": "<b>Delphi<\/b> is a leading global supplier for the automotive, computing <b>...<\/b>",
    "unescapedUrl": "http:\/\/www.delphi.com\/"
   },{
    "titleNoFormatting": "delphi 窯洞",
    "url": "http:\/\/www.51delphi.com\/",
    "cacheUrl": "http:\/\/www.google.com\/search?q=cache:Kgv5hBXmSFgJ:www.51delphi.com",
    "GsearchResultClass": "GwebSearch",
    "visibleUrl": "www.51delphi.com",
    "content": "基於<b>Delphi<\/b> 的Rich Internet Application開發和應用平臺 。 <b>...<\/b> 61449, <b>Delphi<\/b> 7的  Property OldCreateOrder does not exist(1786字節), javer007, 2009-10-12 14:42:48   <b>...<\/b>",
    "title": "<b>delphi<\/b> 窯洞",
    "unescapedUrl": "http:\/\/www.51delphi.com\/"
   },{
    "titleNoFormatting": "delphi專欄_軟件開發_最專業的程序開發網站_開發者網絡_軟件_天極網",
    "url": "http:\/\/dev.yesky.com\/ysdevdelphi\/",
    "cacheUrl": "http:\/\/www.google.com\/search?q=cache:j-qd-w9ESrwJ:dev.yesky.com",
    "GsearchResultClass": "GwebSearch",
    "visibleUrl": "dev.yesky.com",
    "content": "在<b>Delphi<\/b>數據庫應用系統的數據錄入過程當中,有些數據是保存在Excel中的;有些是保存   <b>...<\/b> 爲了便於開發數據庫應用程序,在<b>Delphi<\/b>集成開發環境中,提供了大量的數據庫工具   <b>...<\/b>",
    "title": "<b>delphi<\/b>專欄_軟件開發_最專業的程序開發網站_開發者網絡_軟件_天極網",
    "unescapedUrl": "http:\/\/dev.yesky.com\/ysdevdelphi\/"
   },{
    "titleNoFormatting": "Delphi俱樂部",
    "url": "http:\/\/www.tommstudio.com\/",
    "cacheUrl": "http:\/\/www.google.com\/search?q=cache:9JQ4L48RA1QJ:www.tommstudio.com",
    "GsearchResultClass": "GwebSearch",
    "visibleUrl": "www.tommstudio.com",
    "content": "dephi俱樂部,c#,<b>delphi<\/b>,<b>delphi<\/b>控件,<b>delphi<\/b>源碼,<b>delphi<\/b>書籍,商業源碼,程序開發,源碼  交易,開發,方案, <b>...<\/b> Net,Java,PowerBuilder,PHP,ASP,PERL 免費源碼下載,<b>Delphi<\/b>俱樂部  .",
    "title": "<b>Delphi<\/b>俱樂部",
    "unescapedUrl": "http:\/\/www.tommstudio.com\/"
   }]
 },
 "responseDetails": null
}
)
相關文章
相關標籤/搜索