码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi Json

时间:2015-05-19 16:21:56      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

superobject.pas单元对json的解析非常方便, 力荐

下面演示对如下json的解析

{ "userid": "u0001", "info": [ { "url": "rtmp://192.168.3.159/livepkgr/66?adbe-live-event=liveevent", "lng": 105.97505, "lat": 34.271555, "id": "0001", "memo": "S.H.E 001" },{ "url": "rtmp://192.168.3.159/livepkgr/66?adbe-live-event=liveevent", "lng": 114.658574, "lat": 32.106045, "id": "0002", "memo": "S.H.E 002" },{ "url": "rtmp://192.168.3.159/livepkgr/66?adbe-live-event=liveevent", "lng": 112.818844, "lat": 26.563882, "id": "0003", "memo": "S.H.E 003" },{ "url": "rtmp://192.168.3.159/livepkgr/66?adbe-live-event=liveevent", "lng": 104.687239, "lat": 29.534077, "id": "0004", "memo": "S.H.E 004" }], "area": "sz01" }

 

procedure TForm1.btn1Click(Sender: TObject);
var
  s:String;
  i: Integer;
  jfields,jitems,jo:ISuperObject;
begin
  jitems:=SA([]);
  jo:=SO();
  jo.S[‘area‘] := ‘sz01‘;
  jo.S[‘userid‘] := ‘u0001‘;
  for i := 0 to 3 do
  begin
    jfields:=SO();
    jfields.S[‘id‘]:= Format(‘%.4d‘, [i+1]);
    jfields.D[‘lng‘]:= 102.0909;
    jfields.D[‘lat‘]:= 33.9888;
    jfields.S[‘url‘]:=‘rtmp://192.168.3.159/livepkgr/66?adbe-live-event=liveevent‘;
    jfields.S[‘memo‘]:=‘S.H.E 00‘+ IntToStr(i+1);
    jitems.AsArray.Add(jfields);
  end;
  jo.O[‘info‘]:=jitems;

  s := jo.AsJSon(True, False);
  mmo1.Text := s;
end;

procedure TForm1.btn2Click(Sender: TObject);
var
  jo,m:ISuperobject;
  i,j:Integer;
  tt:TSuperArray;
  s:string;
  lst:TStringList;
begin
  if mmo1.Text <>‘‘ then
  begin
    lst:=TStringList.Create;
    jo:=so(mmo1.Text);
    tt:=jo.O[‘info‘].AsArray;
    for i:=0 to tt.Length-1 do
    begin
      s:=tt.O[i].S[‘id‘];
      lst.Add(s);
    end;
    ShowMessage(lst.Text) ;
    lst.Free;
  end;
end;

点此下载该单元文件  

Delphi Json

标签:

原文地址:http://www.cnblogs.com/doorsky/p/4514781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!