程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> JSON 之 SuperObject(3): 訪問

JSON 之 SuperObject(3): 訪問

編輯:Delphi

測試數據提前加入 Memo1 中:

   [Ctrl+A 全部選擇 提示:你可先修改部分代碼,再按運行]

  代碼文件:

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 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved