DELPHI7下用superobject读取中国天气网的JSON数据的天气预报

这几天做了一个小程序,想读取中国天气网,查了许多资料,终于给弄出来了。参考了不得闲日志中的代码,不过他用的是lkJSON的类库,我用的是superobject。

这里仅作测试,所以直接读取的是绵阳的天气('http://m.weather.com.cn/data/101270401.html),如需查询其它城市,要自己做个城市代码的选择。

返回的JSON数据如下

{"weatherinfo":{"city":"绵阳","city_en":"mianyang","date_y":"2013年8月24日","date":"","week":"星期六","fchh":"08","cityid":"101270401","temp1":"35℃~26℃","temp2":"30℃~25℃","temp3":"33℃~24℃","temp4":"30℃~24℃","temp5":"27℃~24℃","temp6":"26℃~22℃","tempF1":"95℉~78.8℉","tempF2":"86℉~77℉","tempF3":"91.4℉~75.2℉","tempF4":"86℉~75.2℉","tempF5":"80.6℉~75.2℉","tempF6":"78.8℉~71.6℉","weather1":"多云转阴","weather2":"多云转阴","weather3":"多云","weather4":"多云转小雨","weather5":"小雨","weather6":"小雨","img1":"1","img2":"2","img3":"1","img4":"2","img5":"1","img6":"99","img7":"1","img8":"7","img9":"7","img10":"99","img11":"7","img12":"99","img_single":"1","img_title1":"多云","img_title2":"阴","img_title3":"多云","img_title4":"阴","img_title5":"多云","img_title6":"多云","img_title7":"多云","img_title8":"小雨","img_title9":"小雨","img_title10":"小雨","img_title11":"小雨","img_title12":"小雨","img_title_single":"多云","wind1":"旋转风小于3级","wind2":"旋转风小于3级","wind3":"旋转风小于3级","wind4":"旋转风小于3级","wind5":"旋转风小于3级","wind6":"旋转风小于3级","fx1":"旋转风","fx2":"旋转风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"炎热","index_d":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。","index48":"热","index48_d":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。","index_uv":"弱","index48_uv":"弱","index_xc":"适宜","index_tr":"一般","index_co":"很不舒适","st1":"35","st2":"25","st3":"28","st4":"24","st5":"33","st6":"24","index_cl":"较适宜","index_ls":"适宜","index_ag":"不易发"}}

 

DELPHI源码如下:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, StdCtrls,msxml;

type
  TForm1 = class(TForm)
    IdHTTP1: TIdHTTP;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    HttpReq: IXMLHttpRequest;
  end;

 

var
  Form1: TForm1;

implementation
uses DateUtils,superobject;
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  url: string;
  vJson: ISuperObject;
begin

  HttpReq := CoXMLHTTPRequest.Create;

  url := 'http://m.weather.com.cn/data/101270401.html';
  HttpReq.open('Get', Url, False, EmptyParam, EmptyParam);
  HttpReq.send(EmptyParam);//开始搜索
  Url := HttpReq.responseText;
  vjson:=so(url);

  Memo1.Lines.Clear;
  if vJson['weatherinfo'].AsString<>'' then
  begin
    vjson:=so(vJson['weatherinfo'].AsString);
    memo1.Lines.Add('城市:'+vJson['city'].AsString );
    Memo1.Lines.Add('今日天气('+vJson['date_y'].AsString+' '+vjson['week'].asstring+'):');
    Memo1.Lines.Add('     温度:'+vjson['temp1'].asstring);
    Memo1.Lines.Add('     天气:'+vjson['weather1'].asstring);
    Memo1.Lines.Add('     风力:'+vjson['wind1'].asstring);

    Memo1.Lines.Add('明日天气('+FormatDateTime('YYYY年MM月DD日 ',DateUtils.IncDay(now))+'):');
    Memo1.Lines.Add('     温度:'+vjson['temp2'].asstring);
    Memo1.Lines.Add('     天气:'+vjson['weather2'].asstring);
    Memo1.Lines.Add('     风力:'+vjson['wind2'].asstring);

    Memo1.Lines.Add(FormatDateTime('YYYY年MM月DD日 ',DateUtils.IncDay(now,2))+':');
    Memo1.Lines.Add('     温度:'+vjson['temp3'].asstring);
    Memo1.Lines.Add('     天气:'+vjson['weather3'].asstring);
    Memo1.Lines.Add('     风力:'+vjson['wind3'].asstring);

    Memo1.Lines.Add(FormatDateTime('YYYY年MM月DD日 ',DateUtils.IncDay(now,3))+':');
    Memo1.Lines.Add('     温度:'+vjson['temp4'].asstring);
    Memo1.Lines.Add('     天气:'+vjson['weather4'].asstring);
    Memo1.Lines.Add('     风力:'+vjson['wind4'].asstring);

    Memo1.Lines.Add(FormatDateTime('YYYY年MM月DD日 ',DateUtils.IncDay(now,4))+':');
    Memo1.Lines.Add('     温度:'+vjson['temp5'].asstring);
    Memo1.Lines.Add('     天气:'+vjson['weather5'].asstring);
    Memo1.Lines.Add('     风力:'+vjson['wind5'].asstring);
  end;

end;


end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值