Delphi JSON 天气预报

来源:互联网 发布:梵高怎么得的精神 知乎 编辑:程序博客网 时间:2024/05/18 03:09
       DELPHI7代码读取中国天气网JSON数据,实现一个天气预报程序,没有过多美化,只是简单的读取出了指定城市的天气信息,美化一下就是一款漂亮的天气预报软件了。需要了解中国天气网城市代码JSON 串格式和中国天气网城市代码 JSON 数组格式,这两个文件已附上,下载压缩包解压即可。首先下载:
产生下载FlashPlayerControl

主要代码:

procedure Getinidate;var  inifile :TIniFile;     ClientIniFile,Weatherid: string;     i,p1 : Integer;     idhttpweatherid: TIdHTTP;     vJson:ISuperObject;begin  i:= 0;  ClientIniFile:= '.\config.ini';  inifile:= TIniFile.Create(ClientIniFile);  Form1.SsityID :=inifile.ReadString('Weather','cityid','null');end;procedure TForm1.Button1Click(Sender: TObject);var  vJson,vItem: ISuperObject;  IdHTTP1: TIdHTTP;  function getbg(TQ: string):Integer ;  begin    Result:= 1;    if Pos('晴', TQ)>0 then      Result:=1;    if Pos('多云', TQ)>0 then      Result:=2;    if Pos('阴', TQ)>0 then      Result:=3;    if Pos('雨', TQ)>0 then      Result:=4;    if Pos('雷', TQ)>0 then      Result:=5;    if Pos('雪', TQ)>0 then      Result:=7;    if Pos('雾', TQ)>0 then      Result:=8;    if Pos('台', TQ)>0 then      Result:=9;    if Pos('沙', TQ)>0 then      Result:=10;  end;begin  if not Button1.Enabled then    Abort;  Button1.Enabled:= False;  IdHTTP1:= TIdHTTP.Create(nil);  Getinidate;//  MessageDlg(sSityID,  mtInformation, [mbOK], 0);  verPng:='999';  try    vJson:=SO(IdHTTP1.Get('http://m.weather.com.cn/data/'+sSityID+'.html'));    vItem :=SO(IdHTTP1.Get('http://www.weather.com.cn/data/sk/'+sSityID+'.html'));    ImageJT.Picture.LoadFromFile('res\'+vJson['weatherinfo.img1'].AsString+'.png');    lblSS.Caption:=vJson['weatherinfo.week'].AsString+'(今天 实时 '+vItem['weatherinfo.temp'].AsString+'℃)';    LabelCity.Caption:= vJson['weatherinfo.city'].AsString;    lbJTWD.Caption:=vJson['weatherinfo.temp1'].AsString;    lblJTTQ.Caption:= vJson['weatherinfo.weather1'].AsString;    lblJTFX.Caption:= vJson['weatherinfo.wind1'].AsString;    Image5.Tag:= getbg(lblJTTQ.Caption);//    MessageDlg('1',  mtInformation, [mbOK], 0);    imagew1.Picture.LoadFromFile('res\'+vJson['weatherinfo.img3'].AsString+'.png');    week1.Caption:=getweek((now)+1);    lbLWD1.Caption:=vJson['weatherinfo.temp2'].AsString;    lblTQ1.Caption:= vJson['weatherinfo.weather2'].AsString;    LblFZ1.Caption:= vJson['weatherinfo.wind2'].AsString;    image1.Tag:= getbg(lblTQ1.Caption);    week2.Caption:=getweek((now)+2);    imagew2.Picture.LoadFromFile('res\'+vJson['weatherinfo.img5'].AsString+'.png');    lbLWD2.Caption:=vJson['weatherinfo.temp3'].AsString;    lblTQ2.Caption:= vJson['weatherinfo.weather3'].AsString;    LblFZ2.Caption:= vJson['weatherinfo.wind3'].AsString;    image2.Tag:= getbg(lblTQ2.Caption);    week3.Caption:=getweek((now)+3);    imagew3.Picture.LoadFromFile('res\'+vJson['weatherinfo.img7'].AsString+'.png');    lbLWD3.Caption:=vJson['weatherinfo.temp4'].AsString;    lblTQ3.Caption:= vJson['weatherinfo.weather4'].AsString;    LblFZ3.Caption:= vJson['weatherinfo.wind4'].AsString;    image3.Tag:= getbg(lblTQ3.Caption);    week4.Caption:=getweek((now)+4);    imagew4.Picture.LoadFromFile('res\'+vJson['weatherinfo.img9'].AsString+'.png');    lbLWD4.Caption:=vJson['weatherinfo.temp5'].AsString;    lblTQ4.Caption:= vJson['weatherinfo.weather5'].AsString;    LblFZ4.Caption:= vJson['weatherinfo.wind5'].AsString;    image4.Tag:= getbg(lblTQ4.Caption);    IdHTTP1.Disconnect;  except    IdHTTP1.Disconnect;    ImageJT.Picture.LoadFromFile('res\NA.png');    lblSS.Caption:=getweek(now)+'(今天 实时N/A℃)';    LabelCity.Caption:= '--';    lbJTWD.Caption:='N/A';    lblJTTQ.Caption:= 'N/A';    lblJTFX.Caption:= 'N/A';    Image5.Tag:= getbg(lblJTTQ.Caption);    imagew1.Picture.LoadFromFile('res\NA.png');    week1.Caption:=getweek((now)+1);    lbLWD1.Caption:= 'N/A';    lblTQ1.Caption:= 'N/A';    LblFZ1.Caption:= 'N/A';    image1.Tag:= getbg(lblTQ1.Caption);    week2.Caption:=getweek((now)+2);    imagew2.Picture.LoadFromFile('res\NA.png');    lbLWD2.Caption:= 'N/A';    lblTQ2.Caption:= 'N/A';    LblFZ2.Caption:= 'N/A';    image2.Tag:= getbg(lblTQ2.Caption);    week3.Caption:=getweek((now)+3);    imagew3.Picture.LoadFromFile('res\NA.png');    lbLWD3.Caption:= 'N/A';    lblTQ3.Caption:= 'N/A';    LblFZ3.Caption:= 'N/A';    image3.Tag:= getbg(lblTQ3.Caption);    week4.Caption:=getweek((now)+4);    imagew4.Picture.LoadFromFile('res\NA.png');    lbLWD4.Caption:= 'N/A';    lblTQ4.Caption:= 'N/A';    LblFZ4.Caption:= 'N/A';    image4.Tag:= getbg(lblTQ4.Caption);    Button1.Enabled:= true;  end;  TransparentFlashPlayerControl1.Movie:= ExtractFilePath(Application.ExeName)+'WeatherSkin\'+inttostr(image5.Tag)+'\main.swf';  Button1.Enabled:= true;end;