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

JSON 之 SuperObject(4): 增、刪、改

編輯:Delphi

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, StdCtrls; 
 
type 
 TForm1 = class(TForm) 
  Button1: TButton; 
  Button2: TButton; 
  Button3: TButton; 
  procedure Button1Click(Sender: TObject); 
  procedure Button2Click(Sender: TObject); 
  procedure Button3Click(Sender: TObject); 
 end; 
 
var 
 Form1: TForm1; 
 
implementation 
 
{$R *.dfm}  
 
uses SuperObject; 
 
//添加 
procedure TForm1.Button1Click(Sender: TObject); 
var 
 jo: ISuperObject; 
 i: Integer; 
begin 
 jo := TSuperObject.Create; 
 
 for i := 0 to 9 do jo.I['N' + IntToStr(i)] := i; 
 ShowMessage(jo.AsJSon(True)); 
 
 for i := 0 to 9 do jo.S['N' + IntToStr(i)] := IntToStr(i); 
 ShowMessage(jo.AsJSon(True)); 
 
 for i := 0 to 9 do jo.B['N' + IntToStr(i)] := Odd(i); 
 ShowMessage(jo.AsJSon(True)); 
 
 for i := 0 to 9 do jo.D['N' + IntToStr(i)] := i / 2; 
 ShowMessage(jo.AsJSon(True)); 
 
 for i := 0 to 9 do jo.O['N' + IntToStr(i)] := SO('{"A":"B"}'); 
 ShowMessage(jo.AsJSon(True)); 
 
 jo.Clear; 
 ShowMessage(jo.AsJSon(True)); 
end; 
 
//修改與刪除 
procedure TForm1.Button2Click(Sender: TObject); 
var 
 jo: ISuperObject; 
 str: string; 
begin 
 jo := SO('{"Name":"張三", "Age":88}'); 
 ShowMessage(jo.AsJSon(False, False)); 
 
 jo.S['Name'] := '李四'; 
 jo.I['Age'] := 18; 
 ShowMessage(jo.AsJSon(False, False)); 
 
 jo.Delete('Age'); 
 ShowMessage(jo.AsJSon(False, False)); 
 
 jo.D['Size'] := 1.83; 
 ShowMessage(jo.AsJSon(False, False)); 
end; 
 
//TSuperArray 的增、刪、改與查看 
procedure TForm1.Button3Click(Sender: TObject); 
var 
 jo,item: ISuperObject; 
 ja: TSuperArray; 
 i: Integer; 
 str: string; 
begin 
 jo := SO('{"愛好":["編程","打球"]}'); 
 ShowMessage(jo.AsJSon(False, False)); 
 
 jo.A['愛好'].Add(SO('看電視')); 
 ShowMessage(jo.AsJSon(False, False)); 
 
 ja := jo.A['愛好']; 
 ja.Add(SO('游泳')); 
 ShowMessage(jo.AsJSon(False, False)); 
 
 ja.Insert(0, SO('睡覺')); 
 ShowMessage(jo.AsJSon(False, False)); 
 
 ja.Delete(0); 
 ShowMessage(jo.AsJSon(False, False)); 
 
 ja.D[0] := 3.14; 
 ShowMessage(jo.AsJSon(False, False)); 
 
 ja.O[0] := SO('{}'); 
 ShowMessage(jo.AsJSon(False, False)); 
 
 str := ''; 
 for i := 0 to ja.Length - 1 do str := Format('%s %s', [str, ja.S[i]]); 
 ShowMessage(Trim(str)); 
 
 str := ''; 
 for item in jo['愛好'] do str := Format('%s %s', [str, item.AsString]); 
 ShowMessage(Trim(str)); 
 
 ja.Clear; 
 ShowMessage(jo.AsJSon(False, False)); 
end; 
 
end. 


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