Recent

Author Topic: Deserialize JSON with fpjsonrtti  (Read 414 times)

bsod

  • Newbie
  • Posts: 6
Deserialize JSON with fpjsonrtti
« on: November 29, 2024, 01:59:39 pm »
I am a beginner with lamw and i need to deserialize JSON to a Objecj using fpjsonrtti.

But when i use the code below the android app close.

Code: Pascal  [Select][+][-]
  1.    
  2.     LReader := TJSONDeStreamer.Create( nil );
  3.     try
  4.       LReader.Options := [jdoCaseInsensitive];
  5.       LReader.JSONToObject( strJSON {String with JSON data}, FList {Object with TCollection} );
  6.     finally
  7.       LReader.Free;
  8.     end;
  9.  

Any suggestions?

dseligo

  • Hero Member
  • *****
  • Posts: 1418
Re: Deserialize JSON with fpjsonrtti
« Reply #1 on: November 29, 2024, 02:20:01 pm »
Did you create FList somewhere (and free it afterwords)?
Something like:
Code: Pascal  [Select][+][-]
  1. type TYourObject = class(TPersistent) // or something like that
  2. ...
  3. ...
  4.     LReader := TJSONDeStreamer.Create( nil );
  5. FList := TYourObject.Create;
  6.     try
  7.       LReader.Options := [jdoCaseInsensitive];
  8.       LReader.JSONToObject( strJSON {String with JSON data}, FList {Object with TCollection} );
  9.     finally
  10.       LReader.Free;
  11.     end;
  12. ...
  13. FList.Free;

bsod

  • Newbie
  • Posts: 6
Re: Deserialize JSON with fpjsonrtti
« Reply #2 on: November 29, 2024, 02:56:40 pm »
Hi dseligo,

It seems to me that we cannot create a variable in the private session of TAndroidModule.

If i use a local variable i can deserialize the object.

But I need the FList variable to be accessed in more areas of the code.

dseligo

  • Hero Member
  • *****
  • Posts: 1418
Re: Deserialize JSON with fpjsonrtti
« Reply #3 on: November 29, 2024, 03:58:36 pm »
By 'TAndroidModule' you mean descendant of the class jForm?
Like this below?
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { TAndroidModule1 }
  4.  
  5.   TAndroidModule1 = class(jForm)

You can have variable in private section (I have it there and it's working).
Maybe you didn't create your form, and you try to use variables in it.

P.S.: I now see that you wrote private session. Did you mean 'section', or session? If later, what do you mean by private session?
« Last Edit: November 29, 2024, 04:00:29 pm by dseligo »

bsod

  • Newbie
  • Posts: 6
Re: Deserialize JSON with fpjsonrtti
« Reply #4 on: November 29, 2024, 04:16:11 pm »
For example

Code: Pascal  [Select][+][-]
  1.  
  2.   TAndroidModule2 = class(jForm)
  3.     btnBuscar: jButton;
  4.     procedure btnBuscarClick(Sender: TObject);
  5.   private
  6.     {private declarations}
  7.     FList:TClientes;
  8.   public
  9.     {public declarations}
  10.   end;
  11.  
  12. procedure TAndroidModule2.AndroidModule2Create(Sender: TObject);
  13. begin
  14.   FList := TClientes.Create;
  15. end;
  16.  
  17. procedure TAndroidModule2.AndroidModule2Destroy(Sender: TObject);
  18. begin
  19.  
  20.   FList.Free;
  21.  
  22. end;
  23.  
  24. procedure TAndroidModule2.btnBuscarClick(Sender: TObject);
  25. begin
  26.  
  27.     if not Assigned(FList) then
  28.       FList.Create;
  29.  
  30.     LReader := TJSONDeStreamer.Create( nil );
  31.     try
  32.       LReader.Options := [jdoCaseInsensitive];
  33.       LReader.JSONToObject( strJSON {String with JSON data}, FList {FList dont Assigned} );
  34.     finally
  35.       LReader.Free;
  36.     end;
  37.  
  38. end;
  39.  
  40.  

« Last Edit: November 29, 2024, 04:18:06 pm by bsod »

dseligo

  • Hero Member
  • *****
  • Posts: 1418
Re: Deserialize JSON with fpjsonrtti
« Reply #5 on: November 29, 2024, 04:25:31 pm »
I think that OnCreate event doesn't work in LAMW. Put this code in 'OnActivityCreate' event:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule2.AndroidModule2Create(Sender: TObject);
  2. begin
  3.   FList := TClientes.Create;
  4. end;

This is wrong (you need to assign to FList, with :=):
Code: Pascal  [Select][+][-]
  1.     if not Assigned(FList) then
  2.       FList.Create;

It should be like this:
Code: Pascal  [Select][+][-]
  1.     if not Assigned(FList) then
  2.       FList := TClientes.Create;

 

TinyPortal © 2005-2018