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

JSON 之 SuperObject(10): Merge、Clone

編輯:Delphi

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, StdCtrls; 
 
type 
 TForm1 = class(TForm) 
  Button1: TButton; 
  Button2: TButton; 
  procedure Button1Click(Sender: TObject); 
  procedure Button2Click(Sender: TObject); 
 end; 
 
var 
 Form1: TForm1; 
 
implementation 
 
{$R *.dfm}  
 
uses SuperObject; 
 
//Merge 合並 
procedure TForm1.Button1Click(Sender: TObject); 
var 
 jo,jt: ISuperObject; 
begin 
 jo := SO('{X:1}'); 
 jt := SO('{Y:2}'); 
 jo.Merge(jt); 
 ShowMessage(jo.AsJSon); // {"Y":2,"X":1} 
// 
 jo := SO('{X:1, Y:2}'); 
 jt := SO('{Y:3}'); 
 jo.Merge(jt); 
 ShowMessage(jo.AsJSon); // {"Y":3,"X":1} 
// 
 jo := SO('{X:1, Y:2}'); 
 jt := SO('{Y:[5,6,7]}'); 
 jo.Merge(jt); 
 ShowMessage(jo.AsJSon); // {"Y":[5,6,7],"X":1} 
// 
 jo := SO('{A:{x:1, y:2}}'); 
 jt := SO('{A:{m:3, n:4}}'); 
 jo.Merge(jt, True); 
 ShowMessage(jo.AsJSon); // {"A":{"y":2,"x":1,"n":4,"m":3}} 
end; 
 
//Clone 克隆 
procedure TForm1.Button2Click(Sender: TObject); 
var 
 jo,jt: ISuperObject; 
begin 
 jo := SO('{A:{x:1, y:2, z:3}}'); 
 jt := jo.Clone; 
 
 jt.I['A.x'] := 7; 
 jt.I['A.y'] := 8; 
 jt.I['A.z'] := 9; 
 
 ShowMessage(jo.AsJSon); // {"A":{"z":3,"y":2,"x":1}} 
 ShowMessage(jt.AsJSon); // {"A":{"z":9,"y":8,"x":7}} 
 
 //如果不是克隆 
 jo := SO('{A:{x:1, y:2, z:3}}'); 
 jt := jo; 
 
 jt.Delete('A.x'); 
 jt.Delete('A.y'); 
 
 ShowMessage(jo.AsJSon); // {"A":{"z":3}} 
 ShowMessage(jt.AsJSon); // {"A":{"z":3}} 
end; 
 
end. 


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