zoukankan      html  css  js  c++  java
  • JSON与Delphi Object的互换

    Delphi自从增强了RTTI后,语言的可灵活性多大增强,Delphi的dbExpress中提供了DBXJSON,和DBXJSONReflect两个单元,可提供JSON序列化

    下面的例子是实现Delphi实体对象转换为JSON字符串,以及JSON字符串转换为Delphi 实体对象的示例:(在Delphi XE3测试通过)

    unit 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.
  • 相关阅读:
    (2)javascript的基本语法、数据结构、变量
    (1)认识javascript
    CSS 浅析position:relative/absolute定位方式
    jquery实现下拉框多选
    Vue.js not detected
    手机代理调试Charles Proxy和Fiddler
    render函数之jsx应用
    vue组件通信方式(多种方案)
    点击页面空白处地方,隐藏弹窗
    css圆角不圆和1px方案
  • 原文地址:https://www.cnblogs.com/leonkin/p/3298020.html
Copyright © 2011-2022 走看看