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.

```           //看后面

----------------------------------

```

procedure TForm1.Button1Click(Sender: TObject);

var  i:integer;

    StrJson,ff,bb:string;

    JSONObject,root,roott: TJSONObject;

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

  s1,JsonStr: String;

  mm,weather: TJSONArray;

begin

          //uses顶部要添加system.JSON单元

    memo1.clear;

    memo2.clear;

  b := TStringStream.Create('',65001);  //65001是UTF-8

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

  JsonStr:= b.DataString;

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

      for i:=0 to Root.count-1 do

begin

  memo1.lines.add(Root.Get(i).JsonString.toString + ' = ' + Root.Get(i).JsonValue.ToString);

end;

      s1:= Root.GetValue('weatherinfo').ToString;

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

          for i:=0 to Root.count-1 do

begin

  memo2.lines.add(Roott.Get(i).JsonString.toString + ' = ' + Roott.Get(i).JsonValue.ToString);

end;

        bb:=  Roott.GetValue('city').ToString;

      showmessage(bb)

end;

```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容