dedelphi xe5 JSONlphi XE5 JSON与Delphi Object的互换

2018-10-30


uni

<br><div>unit Unit1;</div><div><br /></div><div>interface</div><div><br /></div><div>uses</div><div>&nbsp; Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,</div><div>&nbsp; Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,DBXJSON;</div><div><br /></div><div>type</div><div>&nbsp; TForm1 = class(TForm)</div><div>&nbsp; &nbsp; Button1: TButton;</div><div>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);</div><div>&nbsp; private</div><div>&nbsp; &nbsp; { Private declarations }</div><div>&nbsp; public</div><div>&nbsp; &nbsp; { Public declarations }</div><div>&nbsp; end;</div><div><br /></div><div>var</div><div>&nbsp; Form1: TForm1;</div><div><br /></div><div>implementation</div><div><br /></div><div>{$R *.dfm}</div><div><br /></div><div>const</div><div>&nbsp; GJSONString =</div><div>&nbsp; &nbsp; '{' +</div><div>&nbsp; &nbsp; ' &nbsp; &nbsp;"name": {'+</div><div>&nbsp; &nbsp; ' &nbsp; &nbsp; &nbsp; &nbsp;"A JSON Object": {' +</div><div>&nbsp; &nbsp; ' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"id": "1"' +</div><div>&nbsp; &nbsp; ' &nbsp; &nbsp; &nbsp; &nbsp;},' +</div><div>&nbsp; &nbsp; ' &nbsp; &nbsp; &nbsp; &nbsp;"Another JSON Object": {' +</div><div>&nbsp; &nbsp; ' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"id": "2"' +</div><div>&nbsp; &nbsp; ' &nbsp; &nbsp; &nbsp; &nbsp;}' +</div><div>&nbsp; &nbsp; ' &nbsp; &nbsp;},' +</div><div>&nbsp; &nbsp; ' &nbsp; &nbsp;"totalobjects": "2"' +</div><div>&nbsp; &nbsp; '}';</div><div><br /></div><div>procedure TForm1.Button1Click(Sender: TObject);</div><div>var</div><div>&nbsp; LJSONObject: TJSONObject;</div><div>&nbsp; Value: TJSONValue;</div><div>begin</div><div>&nbsp; LJSONObject := nil;</div><div>&nbsp; try</div><div>&nbsp; &nbsp; LJSONObject := TJsonObject.Create;</div><div>&nbsp; &nbsp; Value := TJSONValue.Create;</div><div>&nbsp; &nbsp; { convert String to JSON }</div><div>&nbsp; &nbsp; LJSONObject.Parse(BytesOf(GJSONString), 0);</div><div>&nbsp; &nbsp; Value :=LJSONObject.GetValue('name');</div><div>&nbsp; &nbsp; ShowMessage(Value.ToString);</div><div>&nbsp; finally</div><div>&nbsp; &nbsp; LJSONObject.Free;</div><div>&nbsp; end;</div><div>end;</div><div><br /></div><div>end.</div><div><span style="font-family:Consolas, 'Courier New', Courier, mono, serif;">http://docwiki.embarcadero.com/RADStudio/XE5/en/JSON</span></div> <br>

t Unit2;

interface

 uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
   DBXJSON, DBXJSONReflect;

 type
   TPerson = class(TObject)
   public
     Name: String;
     Password: String;
     Age: Integer;
   end;

   TForm2 = class(TForm)
     Memo1: TMemo;
     procedure FormCreate(Sender: TObject);
   private
     function ObjectToJSON(AData: TObject): TJSONValue;
     function JSONToObject(AJSONValue: TJSONValue): TObject;
   public                                                    
   end;

 var
   Form2: TForm2;

implementation

 {$R *.dfm}

 function TForm2.JSONToObject(AJSONValue: TJSONValue): TObject;
 var
   lUnMarshal: TJSONUnMarshal;
 begin
   lUnMarshal := TJSONUnMarshal.Create();
   try
     Result := lUnMarshal.Unmarshal(AJSONValue);
   finally
     FreeAndNil(lUnMarshal);
   end;
 end;

 function TForm2.ObjectToJSON(AData: TObject): TJSONValue;
 var
   lMarshal: TJSONMarshal;
 begin
   lMarshal := TJSONMarshal.Create();
   try
     Result := lMarshal.Marshal(AData);
   finally
     FreeAndNil(lMarshal);
   end;
 end;

 procedure TForm2.FormCreate(Sender: TObject);
 var
   lPerson: TPerson;
   lJSONValue: TJSONValue;
 const
   lJSONString: String = '{"type":"Unit2.TPerson","id":1,"fields":{"Name":"Hezihang","Password":"123","Age":23}}';
 begin
   Memo1.Lines.Clear;
   /// Object Convert to JSON
   Memo1.Lines.Add('Object to JSON String');
   Memo1.Lines.Add('--------------------------------------');
   Memo1.Lines.Add('');
   lPerson       := TPerson.Create;
   lPerson.Name    := 'Hezihang';
   lPerson.Password := '123';
   lPerson.Age   := 23;
   lJSONValue       := ObjectToJSON(lPerson);
   FreeAndNil(lPerson);
   Memo1.Lines.Add(lJSONValue.ToString);
   lJSONValue.Free;
   Memo1.Lines.Add('');
   Memo1.Lines.Add('--------------------------------------');
   /// JSON Convert to Object
   Memo1.Lines.Add('');
   Memo1.Lines.Add('JSON String'' To a Class Instance''');
   Memo1.Lines.Add('--------------------------------------');
   Memo1.Lines.Add('');
   lJSONValue := TJSONObject.ParseJSONValue(lJSONString);
   lPerson := JSONToObject(lJSONValue) as TPerson;
   lJSONValue.Free;
   Memo1.Lines.Add('Name: ' + lPerson.Name);
   Memo1.Lines.Add('Password: ' + lPerson.Password);
   Memo1.Lines.Add('Age: ' + IntToStr(lPerson.Age));
   lPerson.Free;
   Memo1.Lines.Add('');
   Memo1.Lines.Add('--------------------------------------');
 end;

end.

来源:http://blog.csdn.net/cmdasm/article/details/12784627


阅读32