zoukankan      html  css  js  c++  java
  • JSON 之 SuperObject(3): 访问


    测试数据提前加入 Memo1 中:


    代码文件:
    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;
        Button5: TButton;
        Button6: TButton;
        Button7: TButton;
        Button8: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure Button5Click(Sender: TObject);
        procedure Button6Click(Sender: TObject);
        procedure Button7Click(Sender: TObject);
        procedure Button8Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses SuperObject;
    
    //数组式访问
    procedure TForm1.Button1Click(Sender: TObject);
    var
      jo: ISuperObject;
    begin
      jo := SO(Memo1.Text);
      ShowMessage(jo['name'].AsString);
      ShowMessage(jo['age'].AsString);
      ShowMessage(jo['sex'].AsString);
      ShowMessage(jo['weight'].AsString);
      ShowMessage(jo['addresses'].AsString);
      ShowMessage(jo['tel'].AsString);
      ShowMessage(jo['children'].AsString);
    end;
    
    //指定返回类型
    procedure TForm1.Button2Click(Sender: TObject);
    var
      jo: ISuperObject;
      s: string;
      i: Integer;
      d: Double;
      b: Boolean;
    begin
      jo := SO(Memo1.Text);
    
      s := jo['name'].AsString;
      i := jo['age'].AsInteger;
      b := jo['sex'].AsBoolean;
      d := jo['weight'].AsDouble;
    
      ShowMessage(s);
      ShowMessage(IntToStr(i));
      ShowMessage(BoolToStr(b, True));
      ShowMessage(FloatToStr(d));
    
      s := jo.S['name'];
      i := jo.I['age'];
      b := jo.B['sex'];
      d := jo.D['weight'];
    
      ShowMessage(s);
      ShowMessage(IntToStr(i));
      ShowMessage(BoolToStr(b, True));
      ShowMessage(FloatToStr(d));
    {
      jo['name'] 是 jo.O['name'] 的简化形式, 因为它是类的默认数组;
      jo.N 类似 jo.O, 区别是 jo.N 在参数不妥时可返回空;
      jo.M 和方法有关, 对象中可嵌入方法.
    }
    end;
    
    //内嵌对象访问
    procedure TForm1.Button3Click(Sender: TObject);
    var
      jo: ISuperObject;
    begin
      jo := SO(Memo1.Text);
    
      ShowMessage(jo['addresses.address'].AsString);
      ShowMessage(jo['addresses.pc'].AsString);
    end;
    
    //TSuperArray
    procedure TForm1.Button4Click(Sender: TObject);
    var
      jo: ISuperObject;
      ja: TSuperArray;
    begin
      jo := SO(Memo1.Text);
      ja := jo['tel'].AsArray;
    
      ShowMessage(IntToStr(ja.Length));
      ShowMessage(ja[0].AsString);
      ShowMessage(ja[1].AsString);
    
      ja := jo['children'].AsArray;
      ShowMessage(ja[0]['name'].AsString);
      ShowMessage(ja[1]['name'].AsString);
    end;
    
    //遍历对象
    procedure TForm1.Button5Click(Sender: TObject);
    var
      item,jo: ISuperObject;
    begin
      jo := SO(Memo1.Text);
      for item in jo do ShowMessage(item.AsJSon(False, False));
    end;
    
    //遍历子对象
    procedure TForm1.Button6Click(Sender: TObject);
    var
      item,jo: ISuperObject;
    begin
      jo := SO(Memo1.Text);
      for item in jo['children'] do
        ShowMessage(item.AsJSon(False, False));
    end;
    
    //使用 TSuperAvlEntry 或 TSuperTableString 遍历
    procedure TForm1.Button7Click(Sender: TObject);
    var
      jo: ISuperObject;
      item: TSuperAvlEntry;
    begin
      jo := SO(Memo1.Text);
      for item in jo.AsObject do
        ShowMessageFmt('Name: %s; Value: %s', [item.Name, item.Value.AsString]);
    end;
    
    //使用 TSuperObjectIter 遍历
    procedure TForm1.Button8Click(Sender: TObject);
    var
      jo: ISuperObject;
      item: TSuperObjectIter;
    begin
      jo := SO(Memo1.Text);
    
      if ObjectFindFirst(jo, item) then
      repeat
        ShowMessageFmt('key: %s; val: %s', [item.key, item.val.AsString]);
      until not ObjectFindNext(item);
      ObjectFindClose(item);
    end;
    
    end.
    
    窗体文件:
    object Form1: TForm1
      Left = 0
      Top = 0
      Caption = 'Form1'
      ClientHeight = 296
      ClientWidth = 377
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object Memo1: TMemo
        Left = 0
        Top = 0
        Width = 281
        Height = 296
        Align = alLeft
        Lines.Strings = (
          '{'
          '  "name": "'#24352#19977'", /* '#27880#37322' */'
          '  "age": 33,'
          '  "sex": true,'
          '  "weight": 123.456,'
          '  "tel": ["86-1111111", "86-2222222"],'
          '  "addresses":{"address":"A'#30465'B'#24066'", "pc":"100001"},'
          '  "children": ['
          '    {'
          '      "name": "'#24352#32487'",'
          '      "age": "22",'
          '      "sex": true'
          '    },'
          '    {'
          '      "name": "'#24352#25215'",'
          '      "age": "11",'
          '      "sex": false'
          '    }'
          '  ]'
          '}')
        ScrollBars = ssBoth
        TabOrder = 0
        ExplicitHeight = 292
      end
      object Button1: TButton
        Left = 287
        Top = 24
        Width = 75
        Height = 25
        Caption = 'Button1'
        TabOrder = 1
        OnClick = Button1Click
      end
      object Button2: TButton
        Left = 287
        Top = 55
        Width = 75
        Height = 25
        Caption = 'Button2'
        TabOrder = 2
        OnClick = Button2Click
      end
      object Button3: TButton
        Left = 287
        Top = 86
        Width = 75
        Height = 25
        Caption = 'Button3'
        TabOrder = 3
        OnClick = Button3Click
      end
      object Button4: TButton
        Left = 287
        Top = 117
        Width = 75
        Height = 25
        Caption = 'Button4'
        TabOrder = 4
        OnClick = Button4Click
      end
      object Button5: TButton
        Left = 287
        Top = 148
        Width = 75
        Height = 25
        Caption = 'Button5'
        TabOrder = 5
        OnClick = Button5Click
      end
      object Button6: TButton
        Left = 287
        Top = 179
        Width = 75
        Height = 25
        Caption = 'Button6'
        TabOrder = 6
        OnClick = Button6Click
      end
      object Button7: TButton
        Left = 287
        Top = 213
        Width = 75
        Height = 25
        Caption = 'Button7'
        TabOrder = 7
        OnClick = Button7Click
      end
      object Button8: TButton
        Left = 287
        Top = 244
        Width = 75
        Height = 25
        Caption = 'Button8'
        TabOrder = 8
        OnClick = Button8Click
      end
    end
    
  • 相关阅读:
    StringUtils工具类的使用
    struts2 文件上传和下载,以及部分源代码解析
    ios开发之猜数字游戏
    从epoll构建muduo-12 多线程入场
    POJ3009 Curling 2.0(DFS)
    IOS-4-面试题1:黑马程序猿IOS面试题大全
    Android-Universal-Image-Loader载入图片
    《UNIX环境高级编程》读书笔记 —— 文件 I/O
    畅通project再续 HDU杭电1875 【Kruscal算法 || Prim】
    轻松学习之Linux教程四 神器vi程序编辑器攻略
  • 原文地址:https://www.cnblogs.com/del/p/1588690.html
Copyright © 2011-2022 走看看