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;

// Array access
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 ;

// Specify the return type
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'] is a simplified form of jo.O ['name'] because it is the default array of classes;
  jo.N is similar to jo.O, except that jo.N can return null if the parameters are incorrect;
  jo.M is related to methods, and methods can be embedded in objects.
}
end ;

// Embedded object access
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 ;

// traverse the object
procedure TForm1.Button5Click (Sender: TObject);
var
  item, jo: ISuperObject;
begin
  jo: = SO (Memo1.Text);
  for itemin jodo ShowMessage (item.AsJSon (False, False));
end ;

// traverse child objects
procedure TForm1.Button6Click (Sender: TObject);
var
  item, jo: ISuperObject;
begin
  jo: = SO (Memo1.Text);
  for itemin jo ['children'] do
    ShowMessage (item.AsJSon (False, False));
end ;

// Use TSuperAvlEntry or TSuperTableString to traverse
procedure TForm1.Button7Click (Sender: TObject);
var
  jo: ISuperObject;
  item: TSuperAvlEntry;
begin
  jo: = SO (Memo1.Text);
  for itemin jo.AsObjectdo 
    ShowMessageFmt ('Name:% s; Value:% s', [item.Name, item.Value.AsString]);
end ;

// Use TSuperObjectIter to traverse
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 .


Form file:

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 = twenty four
    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