Giter VIP home page Giter VIP logo

tjsonobject's Introduction

TJsonObject

Serialize, deserialize json object. Delphi

Save DATA

{$M+}
  TMyTest = class(TObject)
  private
    fInt:Integer;
    fString:String;
  public
   Constructor Create;
   Destructor Free;
  published
    property MyStr: string read fString write fString;
    property MyInt: Integer read fInt write fInt;
  end;
{$M-}

...

procedure TForm1.Button1Click(Sender: TObject);
var
 i:integer;
 SObj:TJsonObject<TMyTest>;
 c:TJsonObject<TMyTest>;
 MyNodeTest:TMyTest;
begin
 SObj:=TJsonObject<TMyTest>.Create;
 // Create data object   // Создаем объект, который хотим сохранить
  MyNodeTest:=TMyTest.Create;
  MyNodeTest.MyStr:='Test JSON serialize, тест';
  MyNodeTest.MyInt:=100;

 // Connect data object to Storage Object // Указываем ссылку на него
	
 SObj.Node:=MyNodeTest;
 // Create SubObject    // Создаем подобъекты (древовидная структура)
 for i:=1 to 10 do
  begin
    c:=TJsonObject<TMyTest>.Create;
     MyNodeTest:=TMyTest.Create;
     MyNodeTest.MyStr:='Str'+inttostr(i);
     MyNodeTest.MyInt:=i;
    c.Node:=MyNodeTest;
    SObj.Storage.Add(c);
  end;
 Memo1.Lines.Text:= SObj.SaveToText;
 SObj.Free;
end;

LoadData

procedure TForm1.Button2Click(Sender: TObject);
var
 SObj,item:TJsonObject<TMyTest>;
begin
 SObj:=TJsonObject<TMyTest>.Create;
 SObj.LoadFromText(Memo1.Lines.Text);
 Memo1.Lines.Add(SObj.Node.MyStr);

// Classic method
 // for i:=0 to SObj.Storage.Count-1 do
  // Memo1.Lines.Add(SObj.Storage[i].Node.MyStr);

// ForIn method
 for item in SObj.ForInStorage do
  Memo1.Lines.Add(item.Node.MyStr);
 SObj.Free;
end;

tjsonobject's People

Contributors

dev-demi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.