JSON 之 SuperObject(16): 实例 - 解析 Google 关键字搜索排名

同上例类似, 通过 'http://clients1.google.cn/complete/search?&q=' + "关键字" 可以获取 Google 的关键字搜索排名.

我用 Delphi 为关键字得到的结果是:


window.google.ac.h(
  ["Delphi",[
      ["delphi 教程", "375,000 结果", "0"],
      ["delphi盒子", "74,900 结果", "1"],
      ["delphi 下载", "1,580,000 结果", "2"],
      ["delphi7 下载", "1,600,000 结果", "3"],
      ["delphi是什么", "497,000 结果", "4"],
      ["delphi 字符串函数", "352,000 结果", "5"],
      ["delphi7 序列号", "302,000 结果", "6"],
      ["delphi2009下载", "20,600 结果", "7"],
      ["delphi7", "1,330,000 结果","8"],
      ["delphi2009正式版下载", "5,710 结果", "9"]
    ]
  ]
)


上面结果以 window.google.ac.h(...) 的形式给出, 下面的操作关键就是给 ISuperObject 一个名为 "window.google.ac.h" 的方法, 并指向自定义的过程, 并在过程中完成解析.



运行效果图:


代码文件:


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;

function ToUTF8Encode(str: string): string;
var
  b: Byte;
begin
  for b in BytesOf(UTF8Encode(str)) do
    Result := Format('%s%s%.2x', [Result, '%', b]);
end;

procedure Proc(const This, Params: ISuperObject; var Result: ISuperObject);
var
  jo: ISuperObject;
begin
  Form1.Memo1.Clear;
  for jo in Params['1'] do with Form1.Memo1.Lines do
    Form1.Memo1.Lines.Add(jo.format('%2%: %0% - %1%'));
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  u = 'http://clients1.google.cn/complete/search?&q=';
var
  jo: ISuperObject;
  req: IXMLHTTPRequest;
  url: WideString;
begin
  jo := SO;
  jo.M['window.google.ac.h'] := @Proc;

  url := u + ToUTF8Encode(Edit1.Text);

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

  jo[req.responseText];
end;

end.
posted @ 2015-09-07 09:20  皮皮鲁PPL  阅读(178)  评论(0编辑  收藏  举报