• 用 SuperObject 解析淘宝上的 Json 数据


    macrolen 给了这样一些数据, 说是用淘宝的 API 获取的 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":"诺基亚 <span class=H>Nokia</span> 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":"诺基亚 三星<span class=H>NOKIA</span> 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":"<span class=H>NOKIA</span> 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":"诺基亚<span class=H>NOKIA</span> 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":"诺基亚<span class=H>NOKIA</span> 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":"诺基亚<span class=H>NOKIA</span> 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":"诺基亚<span class=H>NOKIA</span> 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":"诺基亚<span class=H>NOKIA</span> 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":"诺基亚<span class=H>NOKIA</span> 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":"诺基亚<span class=H>NOKIA</span> 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":"<span class=H>NOKIA</span>手机防骚扰软件,特价!只赚信誉不赚钱!","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":"诺基亚<span class=H>NOKIA</span> 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小时】<span class=H>NOKIA</span> 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":"不干胶 标签/不干胶 印刷/不干胶 贴纸/防伪标签/<span class=H>NOKIA</span>防伪标","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":"不干胶 标签/不干胶 印刷/不干胶 贴纸/防伪标签/<span class=H>NOKIA</span>防伪标","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":"激光不干胶 标签/不干胶 印刷/不干胶 贴纸/激光标签/<span class=H>NOKIA</span>防伪标","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":"<span class=H>NOKIA</span>原装数据线 包原适用大部分型号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":"<span class=H>NOKIA</span>彩铃--等爱的玫瑰","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":"<span class=H>NOKIA</span>彩铃--我爱你塞北的雪","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":"<span class=H>NOKIA</span>挂绳","type":"fixed"}]}},"total_results":189678}}

    把这些数据保存在: c: empjson.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:	empjson.txt';
    begin
      Assert(FileExists(JsonPath), '文件不存在');
      with TStringList.Create do
      begin
        LoadFromFile('c:	empjson.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, 还没有测试它新增的功能, 不过我这个是用它做的.

  • 相关阅读:
    【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件
    二分查找
    MongoDB安装及启动
    Python文件IO(普通文件读写)
    Python面向对象--高级(二)
    Python面向对象--高级(一)
    Python类与对象--基础
    Python生成器、装饰器
    Python函数式编程
    Python函数及参数
  • 原文地址:https://www.cnblogs.com/cpprun/p/4788014.html
Copyright © 2020-2023  润新知