用 SuperObject 解析淘宝上的 Json 数据

{"items_search_response":{"item_search":{"item_categories":{"item_category":[{"category_id":150708,"count":66},{"category_id":50002480,"count":93},{"category_id":50006247,"count":164},{"category_id":50012579,"count":19518},{"category_id":50012018,"count":92},{"category_id":150704,"count":10527},{"category_id":150705,"count":226},{"category_id":150706,"count":604},{"category_id":50005050,"count":2900},{"category_id":50011418,"count":94},{"category_id":50012587,"count":6711},{"category_id":50012584,"count":14661},{"category_id":50011150,"count":3058},{"category_id":50008194,"count":72},{"category_id":50010259,"count":348},{"category_id":50008681,"count":251},{"category_id":50003774,"count":252},{"category_id":50003775,"count":7022},{"category_id":150701,"count":802},{"category_id":50012010,"count":134},{"category_id":50008202,"count":68},{"category_id":3412,"count":1125},{"category_id":50005787,"count":127},{"category_id":50002928,"count":158},{"category_id":50014945,"count":113},{"category_id":50010614,"count":2120},{"category_id":140908,"count":122},{"category_id":50003781,"count":349},{"category_id":50008734,"count":755},{"category_id":50002887,"count":114},{"category_id":50003853,"count":252},{"category_id":50005266,"count":2490},{"category_id":50003327,"count":1326},{"category_id":50008169,"count":562},{"category_id":1205,"count":202},{"category_id":50008096,"count":66},{"category_id":50005729,"count":752},{"category_id":50012166,"count":176},{"category_id":1512,"count":107960},{"category_id":111005,"count":330}]},"items":{"item":[{"cid":150701,"delist_time":"2010-01-20 19:07:23","iid":"6b1589a0bbcd485c32e3c41280dbe7a1","location":{"city":"襄樊","state":"湖北"},"nick":"勤劳的蚂蚁","pic_url":"http://img05.taobaocdn.com/bao/uploaded/i5/T1ER4mXfNhXXbBnmZV_022002.jpg","post_fee":"10.00","price":"0.01","title":"诺基亚 Nokia 5800 5802原配挂绳  拨片","type":"fixed"},{"cid":111005,"delist_time":"2010-01-27 22:18:15","iid":"70fe721c9586bba64003071cc91fbf76","location":{"city":"武汉","state":"湖北"},"nick":"whjctx","pic_url":"http://img04.taobaocdn.com/bao/uploaded/i4/T13IJlXa4SF0O8eUk8_101815.jpg","post_fee":"0.00","price":"0.01","title":"诺基亚 三星NOKIA S60赛班系统证书/数字证书制作/赛班证书制作/","type":"fixed"},{"cid":50005981,"delist_time":"2010-01-28 14:25:13","iid":"66367f673e58f63878ea69736fcd2f5f","location":{"city":"广州","state":"广东"},"nick":"habit5200","pic_url":"http://img06.taobaocdn.com/bao/uploaded/i6/T1mylkXlH2AJNeW1s1_041006.jpg","post_fee":"0.00","price":"0.01","title":"NOKIA N97待机图片","type":"fixed"},{"cid":50012587,"delist_time":"2010-01-28 21:00:00","iid":"2c71fb0ca2dba9f3ae5c01f12f98ed73","location":{"city":"广州","state":"广东"},"nick":"mostmoral","pic_url":"http://img07.taobaocdn.com/bao/uploaded/i7/T1p1lrXiFxXXbZq0o2_043505.jpg","post_fee":"8.00","price":"0.01","title":"诺基亚NOKIA 5530 触屏手机 屏幕保护膜/贴膜/屏保膜 非镜面膜","type":"fixed"},{"cid":50012587,"delist_time":"2010-01-29 00:00:00","iid":"226514eb9283ec20695043e27c1a60e8","location":{"city":"广州","state":"广东"},"nick":"mostmoral","pic_url":"http://img02.taobaocdn.com/bao/uploaded/i2/T1MuprXd8vXXa5Dwc1_040919.jpg","post_fee":"8.00","price":"0.01","title":"诺基亚NOKIA E63专用智能手机 屏幕保护膜/贴膜/屏保膜非镜面膜","type":"fixed"},{"cid":50012587,"delist_time":"2010-01-30 00:00:00","iid":"e0b8a84070297bfe6179e9d2acebaae3","location":{"city":"广州","state":"广东"},"nick":"mostmoral","pic_url":"http://img01.taobaocdn.com/bao/uploaded/i1/T1Ge8rXe0vXXazcT7Y_030713.jpg","post_fee":"8.00","price":"0.01","title":"诺基亚NOKIA N82 直板手机专用屏幕保护膜/贴膜/屏保膜 非镜面膜","type":"fixed"},{"cid":50012587,"delist_time":"2010-01-30 09:00:00","iid":"bf433c0ca19c9070635ad46628f367eb","location":{"city":"广州","state":"广东"},"nick":"mostmoral","pic_url":"http://img04.taobaocdn.com/bao/uploaded/i4/T13u0rXX0yXXXZdbM3_045925.jpg","post_fee":"8.00","price":"0.01","title":"诺基亚NOKIA 8800A 滑盖手机 屏幕保护膜/贴膜/屏保膜 非镜面膜","type":"fixed"},{"cid":50012587,"delist_time":"2010-01-30 15:00:00","iid":"1f0fdb5181209632623eb0e3b6c84e5f","location":{"city":"广州","state":"广东"},"nick":"mostmoral","pic_url":"http://img01.taobaocdn.com/bao/uploaded/i1/T1nvxrXnxlXXa.hmZ2_045316.jpg","post_fee":"8.00","price":"0.01","title":"诺基亚NOKIA N78 直板手机专用屏幕保护膜/贴膜/屏保膜 非镜面膜","type":"fixed"},{"cid":50012587,"delist_time":"2010-01-31 00:00:00","iid":"4c255a73b0b4ea8f8c06655214265b45","location":{"city":"广州","state":"广东"},"nick":"mostmoral","pic_url":"http://img06.taobaocdn.com/bao/uploaded/i6/T1zvtrXaFnXXXFOEQ2_045546.jpg","post_fee":"8.00","price":"0.01","title":"诺基亚NOKIA E66专用滑盖手机 屏幕保护膜/贴膜/屏保膜 非镜面膜","type":"fixed"},{"cid":50012587,"delist_time":"2010-01-31 09:00:00","iid":"a88b1bfec52d12e4d7b210d73d6911ff","location":{"city":"广州","state":"广东"},"nick":"mostmoral","pic_url":"http://img03.taobaocdn.com/bao/uploaded/i3/T1FvtrXoVmXXXPrgE3_050142.jpg","post_fee":"8.00","price":"0.01","title":"诺基亚NOKIA N96 双向滑盖手机 屏幕保护膜/贴膜/屏保膜 非镜面膜","type":"fixed"},{"cid":50003853,"delist_time":"2010-01-31 16:28:05","iid":"77fab0d06ff871131c594c002f839cac","location":{"city":"合肥","state":"安徽"},"nick":"baoyuanfei","pic_url":"http://img08.taobaocdn.com/bao/uploaded/i8/T1hv0qXb0yXXcTmX.._111221.jpg","post_fee":"0.00","price":"0.01","title":"NOKIA手机防骚扰软件,特价!只赚信誉不赚钱!","type":"fixed"},{"cid":50012587,"delist_time":"2010-02-01 09:00:00","iid":"38fc1c7bcc12bfe0e7ea3baa819490d1","location":{"city":"广州","state":"广东"},"nick":"mostmoral","pic_url":"http://img01.taobaocdn.com/bao/uploaded/i1/T1pxtrXgFdXXbVNIHa_120423.jpg","post_fee":"8.00","price":"0.01","title":"诺基亚NOKIA N95 8GB 滑盖手机 屏幕保护膜/贴膜/屏保膜 非镜面膜","type":"fixed"},{"cid":50002480,"delist_time":"2010-01-19 14:13:27","iid":"341c2ba20d491fe92af6e48dbf3b6590","location":{"city":"秦皇岛","state":"河北"},"nick":"zhouyunzhe520","pic_url":"http://img02.taobaocdn.com/bao/uploaded/i2/T1EjXlXfWpIJOuh2HX_084849.jpg","post_fee":"0.00","price":"0.10","title":"【1小时】NOKIA E66手机证书制作 塞班 17最高权限","type":"fixed"},{"cid":50011150,"delist_time":"2010-01-20 00:10:57","iid":"2ec4d847b7d70a331d6ba59cb7ba6da2","location":{"city":"温州","state":"浙江"},"nick":"星星亮星","pic_url":"http://img07.taobaocdn.com/bao/uploaded/i7/T1WfRgXbXBXXbIjtEW_022836.jpg","post_fee":"0.00","price":"0.10","title":"不干胶 标签/不干胶 印刷/不干胶 贴纸/防伪标签/NOKIA防伪标","type":"fixed"},{"cid":50011150,"delist_time":"2010-01-20 14:23:52","iid":"b221417baa9905dd7c7181b8b0cde710","location":{"city":"温州","state":"浙江"},"nick":"星星亮星","pic_url":"http://img01.taobaocdn.com/bao/uploaded/i1/T1zMXgXi4AXXcVWJgW_022806.jpg","post_fee":"0.00","price":"0.10","title":"不干胶 标签/不干胶 印刷/不干胶 贴纸/防伪标签/NOKIA防伪标","type":"fixed"},{"cid":50011150,"delist_time":"2010-01-20 18:46:54","iid":"1e856e0507266ad417768ff397f5e993","location":{"city":"温州","state":"浙江"},"nick":"星星亮星","pic_url":"http://img01.taobaocdn.com/bao/uploaded/i1/T1EgtgXeJzXXXjkGAW_022444.jpg","post_fee":"0.00","price":"0.10","title":"激光不干胶 标签/不干胶 印刷/不干胶 贴纸/激光标签/NOKIA防伪标","type":"fixed"},{"cid":50003327,"delist_time":"2010-01-20 23:27:13","iid":"61412071aaeeb580f03240987ba31e33","location":{"city":"南京","state":"江苏"},"nick":"小不点名绣坊","pic_url":"http://img01.taobaocdn.com/bao/uploaded/i1/T1vkxrXdpbXXcN8yZ8_070829.jpg","post_fee":"5.00","price":"0.10","title":"NOKIA原装数据线 包原适用大部分型号5800,E71,5530,N97,N86等","type":"fixed"},{"cid":50005980,"delist_time":"2010-01-21 09:43:00","iid":"f40ee96c548b3adbc38a080c39e33130","location":{"city":"上海","state":"上海"},"nick":"seazhang8421","pic_url":"http://img01.taobaocdn.com/bao/uploaded/i1/T1WZxnXlJkXXXQwcvX_114450.jpg","post_fee":"0.00","price":"0.10","title":"NOKIA彩铃--等爱的玫瑰","type":"fixed"},{"cid":50005980,"delist_time":"2010-01-21 09:43:00","iid":"443dc0516f48677c20c4801e47d6732c","location":{"city":"上海","state":"上海"},"nick":"seazhang8421","pic_url":"http://img02.taobaocdn.com/bao/uploaded/i2/T18c8hXdeW_tKFc.75_060404.jpg","post_fee":"0.00","price":"0.10","title":"NOKIA彩铃--我爱你塞北的雪","type":"fixed"},{"cid":50011150,"delist_time":"2010-01-21 17:02:14","iid":"08e0a507c9dceb327bb66a40da1cfab2","location":{"city":"成都","state":"四川"},"nick":"cdzysd","pic_url":"http://img08.taobaocdn.com/bao/uploaded/i8/T125JlXbVJItP8_w73_050214.jpg","post_fee":"10.00","price":"0.10","title":"NOKIA挂绳","type":"fixed"}]}},"total_results":189678}}

把这些数据保存在: c:\temp\json.txt; 然后在窗体上放一个 Memo 和四个 Button …

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses SuperObject;
var jo: ISuperObject;

//载入并建立对象
procedure TForm1.FormCreate(Sender: TObject);
const
  JsonPath = 'c:\temp\json.txt';
begin
  Assert(FileExists(JsonPath), '文件不存在');
  with TStringList.Create do
  begin
    LoadFromFile('c:\temp\json.txt');
    jo := SO(Text);
    Free;
  end;
end;

//按格式查看所有
procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Text := jo.AsJSon(True, False);
end;

//查看: items_search_response.item_search.item_categories
procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Text := jo['items_search_response.item_search.item_categories'].AsJSon(True, False);
end;

//查看: items_search_response.item_search.items
procedure TForm1.Button3Click(Sender: TObject);
begin
  Memo1.Text := jo['items_search_response.item_search.items'].AsJSon(True, False);
end;

//遍历 items_search_response.item_search.items.item 数组中的指定元素
procedure TForm1.Button4Click(Sender: TObject);
var
  ja: TSuperArray;
  i: Integer;
begin
  ja := jo['items_search_response.item_search.items.item'].AsArray;
  Memo1.Clear;
  for i := 0 to ja.Length - 1 do
  begin
    Memo1.Lines.Add(ja.O[i].Format('%cid%; %location.state%; %location.city%'));
  end;
end;

end.


另外 SuperObject 当前的最新版本是: SuperObjectv1.2.3, 还没有测试它新增的功能, 不过我这个是用它做的.
万一
关注 - 33
粉丝 - 126
关注博主
0
0
0
(请您对文章做出评价)
« 上一篇:一句话获取文件的最新修改时间
» 下一篇:使用多窗体时, 关于节约内存和加快启动速度的思考与尝试
posted on 2010-01-18 18:18 万一 阅读(1026) 评论(10) 编辑 收藏 所属分类: 001.问与答, 47.Delphi 与 JOSN

FeedBack:
1777908
#1楼 2010-01-18 18:49 delphing      
万一老师 想问一个关于 RegularExpressions 正则表达式的问题

Memo1.Text := HTTP.Get('http://showgame.game176.com/Point/GetCardByMobile.aspx');

procedure TForm1.Button2Click(Sender: TObject);
var
Reg: IRegex;
Match: IMatch;
begin
Reg := TRegex.Create('__VIEWSTATE" value\="(.*?)" />', [roMultiline]);
Match := Reg.Match(Memo1.Text);
while Match.Success do
begin
Memo2.Lines.Add('__VIEWSTATE '+Match.Groups[1].Value);
Match := Match.NextMatch;
end;
end;



发表评论

电子邮件地址不会被公开。 必填项已用*标注

4 + 2 =