2018-09-28 json 气象

unit Unit3;

interface

uses

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,system.json,Vcl.StdCtrls,

  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;

type

  TForm3 = class(TForm)

    Memo1: TMemo;

    Button1: TButton;

    IdHTTP1: TIdHTTP;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);

var

    b:Tstringstream;  //用于接收json数据的流

    s1:string;

    Root,root1:TJSONObject;

begin

    b:=Tstringstream.create('',65001);

    //65001是UTF-8 ,TIdHttp在获取数据之前,要将定义的TStringStream的Encoding设置为UTF8,才可以。不使用会乱码

    idhttp1.get('http://www.weather.com.cn/data/cityinfo/101010100.html',b); //获取中国气象局公开的json数据

    s1:=b.datastring;

    memo1.Lines.Add(s1);

    Root:= TJSONObject.ParseJSONValue(Trim(s1)) as TJSONObject;

        root1:= TJSONObject.ParseJSONValue(root.GetValue('weatherinfo').ToString) as TJSONObject;

            showmessage( root1.GetValue('city').ToString +  root1.GetValue('weather').ToString  );

end;

end.

你可能感兴趣的:(2018-09-28 json 气象)