Recent

Author Topic: Utilisation JSON  (Read 1113 times)

essam eddine adib

  • New Member
  • *
  • Posts: 20
Utilisation JSON
« on: September 14, 2019, 01:32:46 pm »
Bonjour,

it first time I use JSON and i have a pb
C'est la première que j'utilise JSON et je rencontre un pb que je ne comprend pas

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   TIngredient = class(TPersistent) // class for the property 'obj' and the TCollection
  4. private
  5.   fid_ingredient: String;
  6.   fnom_ingredient: String;
  7.   Fprix_ingredient: Currency;
  8. published
  9.   property id_ingredient : String read fid_ingredient write fid_ingredient;
  10.   property nom_ingredient : String read fnom_ingredient write fnom_ingredient;
  11.   property prix_ingredient : Currency read Fprix_ingredient write Fprix_ingredient;
  12. end;
  13.  
  14.  
  15.  
  16.   { TProduit }
  17.  
  18. TProduit = class(TCollectionItem) // class for the property 'obj' and the TCollection
  19. private
  20.   fid_produit: String;
  21.   fnom_produit: String;
  22.   Fqte: Double;
  23.   Fprixp: Currency;
  24.   fingredient : TIngredient;
  25. public
  26.   constructor Create;
  27.   destructor Destroy; override;
  28. published
  29.   property id_produit : String read fid_produit write fid_produit;
  30.   property nom_produit : String read fnom_produit write fnom_produit;
  31.   property qte : Double read Fqte write Fqte;
  32.   property prix : Currency read Fprixp write Fprixp;
  33.   property ingredient : TIngredient read fingredient;
  34.   //property total_commande: Currency read Ftotal_commande write Ftotal_commande;
  35.   //property client: TStrings read fclient;
  36. end;
  37.  
  38. { TProduits }
  39.  
  40. TProduits = class(TCollection) // class for the property 'obj' and the TCollection
  41. private
  42.   Fproduit: TProduit;
  43. public
  44.   constructor Create;
  45.   destructor Destroy; override;
  46. published
  47.   property produit : TProduit read Fproduit write Fproduit;
  48. end;
  49.  
  50.  
  51.  
  52.  
  53.   TVille = class(TPersistent) // class for the property 'obj' and the TCollection
  54. private
  55.   fid_ville: String;
  56.   //fdate_commande: TDate;
  57.   //ftotal_commande: Currency;
  58.   fnom_ville: String;
  59. published
  60.   property id_ville : String read fid_ville write fid_ville;
  61.   property nom_ville : String read fnom_ville write fnom_ville;
  62.   //property total_commande: Currency read Ftotal_commande write Ftotal_commande;
  63.   //property client: TStrings read fclient;
  64. end;
  65.  
  66.   { TClient }
  67.  
  68.   TClient = class(TPersistent) // class for the property 'obj' and the TCollection
  69. private
  70.   fid_client: String;
  71.   fnom : String;
  72.   //ftotal_commande: Currency;
  73. //  fpayment: TStrings;
  74.   fville : TVille;
  75. public
  76.   constructor Create;
  77.   destructor Destroy; override;
  78. published
  79.   property id_client : String read fid_client write fid_client;
  80.   property nom : String read fnom write fnom;
  81. //  property payment : TStrings read fpayment;
  82.   property ville : TVille read fville;
  83.   //property date_commande: TDate read Fdate_commande write Fdate_commande;
  84.   //property total_commande: Currency read Ftotal_commande write Ftotal_commande;
  85.   //property client: TStrings read fclient;
  86. end;
  87.  
  88.  
  89.  
  90.   { TResult }
  91.  
  92.   TResult = class(TPersistent) // class for the property 'obj' and the TCollection
  93.   private
  94.     fid_commande: String;
  95.     fdate_commande: String;
  96.     ftotal_commande: Currency;
  97.     fclient: TClient;
  98.     fproduits: TCollection;
  99.     fpayment : TStrings;
  100.   public
  101.     constructor Create;
  102.     destructor Destroy; override;
  103.   published
  104.     property id_commande : String read Fid_commande write Fid_commande;
  105.     property date_commande: String read Fdate_commande write Fdate_commande;
  106.     property total_commande: Currency read Ftotal_commande write Ftotal_commande;
  107.     property client: TClient read fclient write fclient;
  108.     property payment: TStrings read fpayment;
  109.     property produits: TCollection read fproduits ;
  110.   end;
  111.  
  112.  
  113.  
  114.   { TBaseObject }
  115.  
  116.   TBaseObject = class(TPersistent)  // class for the JSON structure
  117.   private
  118.     fcode: String;
  119.     fmessage: String;
  120.     fresult: TResult;
  121.   public
  122.     constructor Create;
  123.     destructor Destroy; override;
  124.   published  // all properties have published
  125.     property code: String read fcode write fcode;
  126.     property message: String read fmessage write fmessage;
  127.     property result: TResult read fresult write fresult;
  128.   end;
  129.  
  130.  
  131.  
  132. implementation
  133.  
  134. {$R *.lfm}
  135.  
  136. { TProduits }
  137.  
  138. constructor TProduits.Create;
  139. begin
  140.   Fproduit:=TProduit.Create;
  141. end;
  142.  
  143. destructor TProduits.Destroy;
  144. begin
  145.   inherited Destroy;
  146. end;
  147.  
  148.  
  149. { TProduit }
  150.  
  151. constructor TProduit.Create;
  152. begin
  153.   fingredient:=TIngredient.Create;
  154. end;
  155.  
  156. destructor TProduit.Destroy;
  157. begin
  158.   fingredient.Free;
  159.   inherited Destroy;
  160. end;
  161.  
  162. { TClient }
  163.  
  164. constructor TClient.Create;
  165. begin
  166.   fville:= TVille.Create;
  167. end;
  168.  
  169. destructor TClient.Destroy;
  170. begin
  171.   inherited Destroy;
  172. end;
  173.  
  174. { TResult }
  175.  
  176. constructor TResult.Create;
  177. begin
  178.   fclient := TClient.Create;
  179.   fproduits := TCollection.Create(TProduit);
  180.   fpayment := TStringList.Create;
  181. end;
  182.  
  183. destructor TResult.Destroy;
  184. begin
  185.   fclient.Free;
  186.   inherited Destroy;
  187. end;
  188.  
  189. { TBaseObject }
  190.  
  191. constructor TBaseObject.Create;
  192. begin
  193.   fResult     := TResult.Create;
  194. end;
  195.  
  196. destructor TBaseObject.Destroy;
  197. begin
  198.   fResult.Free;
  199.   inherited Destroy;
  200. end;
  201.  
  202.  
  203. procedure TForm1.DeStreamTest;
  204. var
  205.   DeStreamer: TJSONDeStreamer;
  206.   o: TBaseObject;
  207.   no: TResult;
  208.   pd : TProduit;
  209.   s: String;
  210.   Ing : TIngredient;
  211. begin
  212.   Memo1.lines.add('DeStream test');
  213.   Memo1.lines.add('======================================');
  214.  
  215.   // DeStreamer object and target object create
  216.   DeStreamer := TJSONDeStreamer.Create(nil);
  217.   o := TBaseObject.Create;
  218.   try
  219.     DeStreamer.JSONToObject(Memo2.Text, o);
  220.     Memo1.lines.add(o.code);
  221.     Memo1.lines.add(o.message);
  222.     Memo1.lines.add('ID Commande : '+o.result.id_commande);
  223.     Memo1.lines.add('Toatal Commande : '+CurrToStr(o.result.total_commande));
  224.     Memo1.lines.add('Client : Code : '+o.result.client.id_client);
  225.     Memo1.lines.add('Client : Nom : '+o.result.client.nom);
  226.     Memo1.lines.add('Client : Ville : Code : '+o.result.client.ville.id_ville);
  227.     Memo1.lines.add('Client : Ville : nom ville : '+o.result.client.ville.nom_ville);
  228.     for s in o.result.payment do
  229.       Memo1.lines.add('Client : Payement  : '+ s);
  230.     for TCollectionItem(pd) in o.result.produits do
  231.      Begin
  232.      Memo1.lines.add('Produit : '+pd.qte.ToString()+' '+pd.nom_produit+' Px: '+CurrToStr(pd.prix));
  233.      Memo1.lines.add('Produit : Ingrédient : '+pd.ingredient.nom_ingredient+' Px: '+CurrToStr(pd.ingredient.prix_ingredient));
  234.      end;
  235.  
  236.   finally
  237.     o.Destroy;
  238.     DeStreamer.Destroy;
  239.   end;
  240. end;
  241.  
  242.  
  243.  
i have message
un message apparait

Code: Text  [Select][+][-]
  1. "unsupported json type for object property  jtArray"


when I add TIngredient to TProduit
lorsque dans le TProduit j'ajoute un type de TIngredient

Thans for help
Merci pour votre aide
« Last Edit: September 14, 2019, 01:47:04 pm by essam eddine adib »

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Utilisation JSON
« Reply #1 on: September 18, 2019, 03:41:18 am »
Bonjour,

it first time I use JSON and i have a pb
C'est la première que j'utilise JSON et je rencontre un pb que je ne comprend pas

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   TIngredient = class(TPersistent) // class for the property 'obj' and the TCollection
  4. private
  5.   fid_ingredient: String;
  6.   fnom_ingredient: String;
  7.   Fprix_ingredient: Currency;
  8. published
  9.   property id_ingredient : String read fid_ingredient write fid_ingredient;
  10.   property nom_ingredient : String read fnom_ingredient write fnom_ingredient;
  11.   property prix_ingredient : Currency read Fprix_ingredient write Fprix_ingredient;
  12. end;
  13.  
  14.  
  15.  
  16.   { TProduit }
  17.  
  18. TProduit = class(TCollectionItem) // class for the property 'obj' and the TCollection
  19. private
  20.   fid_produit: String;
  21.   fnom_produit: String;
  22.   Fqte: Double;
  23.   Fprixp: Currency;
  24.   fingredient : TIngredient;
  25. public
  26.   constructor Create;
  27.   destructor Destroy; override;
  28. published
  29.   property id_produit : String read fid_produit write fid_produit;
  30.   property nom_produit : String read fnom_produit write fnom_produit;
  31.   property qte : Double read Fqte write Fqte;
  32.   property prix : Currency read Fprixp write Fprixp;
  33.   property ingredient : TIngredient read fingredient;
  34.   //property total_commande: Currency read Ftotal_commande write Ftotal_commande;
  35.   //property client: TStrings read fclient;
  36. end;
  37.  
  38. { TProduits }
  39.  
  40. TProduits = class(TCollection) // class for the property 'obj' and the TCollection
  41. private
  42.   Fproduit: TProduit;
  43. public
  44.   constructor Create;
  45.   destructor Destroy; override;
  46. published
  47.   property produit : TProduit read Fproduit write Fproduit;
  48. end;
  49.  
  50.  
  51.  
  52.  
  53.   TVille = class(TPersistent) // class for the property 'obj' and the TCollection
  54. private
  55.   fid_ville: String;
  56.   //fdate_commande: TDate;
  57.   //ftotal_commande: Currency;
  58.   fnom_ville: String;
  59. published
  60.   property id_ville : String read fid_ville write fid_ville;
  61.   property nom_ville : String read fnom_ville write fnom_ville;
  62.   //property total_commande: Currency read Ftotal_commande write Ftotal_commande;
  63.   //property client: TStrings read fclient;
  64. end;
  65.  
  66.   { TClient }
  67.  
  68.   TClient = class(TPersistent) // class for the property 'obj' and the TCollection
  69. private
  70.   fid_client: String;
  71.   fnom : String;
  72.   //ftotal_commande: Currency;
  73. //  fpayment: TStrings;
  74.   fville : TVille;
  75. public
  76.   constructor Create;
  77.   destructor Destroy; override;
  78. published
  79.   property id_client : String read fid_client write fid_client;
  80.   property nom : String read fnom write fnom;
  81. //  property payment : TStrings read fpayment;
  82.   property ville : TVille read fville;
  83.   //property date_commande: TDate read Fdate_commande write Fdate_commande;
  84.   //property total_commande: Currency read Ftotal_commande write Ftotal_commande;
  85.   //property client: TStrings read fclient;
  86. end;
  87.  
  88.  
  89.  
  90.   { TResult }
  91.  
  92.   TResult = class(TPersistent) // class for the property 'obj' and the TCollection
  93.   private
  94.     fid_commande: String;
  95.     fdate_commande: String;
  96.     ftotal_commande: Currency;
  97.     fclient: TClient;
  98.     fproduits: TCollection;
  99.     fpayment : TStrings;
  100.   public
  101.     constructor Create;
  102.     destructor Destroy; override;
  103.   published
  104.     property id_commande : String read Fid_commande write Fid_commande;
  105.     property date_commande: String read Fdate_commande write Fdate_commande;
  106.     property total_commande: Currency read Ftotal_commande write Ftotal_commande;
  107.     property client: TClient read fclient write fclient;
  108.     property payment: TStrings read fpayment;
  109.     property produits: TCollection read fproduits ;
  110.   end;
  111.  
  112.  
  113.  
  114.   { TBaseObject }
  115.  
  116.   TBaseObject = class(TPersistent)  // class for the JSON structure
  117.   private
  118.     fcode: String;
  119.     fmessage: String;
  120.     fresult: TResult;
  121.   public
  122.     constructor Create;
  123.     destructor Destroy; override;
  124.   published  // all properties have published
  125.     property code: String read fcode write fcode;
  126.     property message: String read fmessage write fmessage;
  127.     property result: TResult read fresult write fresult;
  128.   end;
  129.  
  130.  
  131.  
  132. implementation
  133.  
  134. {$R *.lfm}
  135.  
  136. { TProduits }
  137.  
  138. constructor TProduits.Create;
  139. begin
  140.   Fproduit:=TProduit.Create;
  141. end;
  142.  
  143. destructor TProduits.Destroy;
  144. begin
  145.   inherited Destroy;
  146. end;
  147.  
  148.  
  149. { TProduit }
  150.  
  151. constructor TProduit.Create;
  152. begin
  153.   fingredient:=TIngredient.Create;
  154. end;
  155.  
  156. destructor TProduit.Destroy;
  157. begin
  158.   fingredient.Free;
  159.   inherited Destroy;
  160. end;
  161.  
  162. { TClient }
  163.  
  164. constructor TClient.Create;
  165. begin
  166.   fville:= TVille.Create;
  167. end;
  168.  
  169. destructor TClient.Destroy;
  170. begin
  171.   inherited Destroy;
  172. end;
  173.  
  174. { TResult }
  175.  
  176. constructor TResult.Create;
  177. begin
  178.   fclient := TClient.Create;
  179.   fproduits := TCollection.Create(TProduit);
  180.   fpayment := TStringList.Create;
  181. end;
  182.  
  183. destructor TResult.Destroy;
  184. begin
  185.   fclient.Free;
  186.   inherited Destroy;
  187. end;
  188.  
  189. { TBaseObject }
  190.  
  191. constructor TBaseObject.Create;
  192. begin
  193.   fResult     := TResult.Create;
  194. end;
  195.  
  196. destructor TBaseObject.Destroy;
  197. begin
  198.   fResult.Free;
  199.   inherited Destroy;
  200. end;
  201.  
  202.  
  203. procedure TForm1.DeStreamTest;
  204. var
  205.   DeStreamer: TJSONDeStreamer;
  206.   o: TBaseObject;
  207.   no: TResult;
  208.   pd : TProduit;
  209.   s: String;
  210.   Ing : TIngredient;
  211. begin
  212.   Memo1.lines.add('DeStream test');
  213.   Memo1.lines.add('======================================');
  214.  
  215.   // DeStreamer object and target object create
  216.   DeStreamer := TJSONDeStreamer.Create(nil);
  217.   o := TBaseObject.Create;
  218.   try
  219.     DeStreamer.JSONToObject(Memo2.Text, o);
  220.     Memo1.lines.add(o.code);
  221.     Memo1.lines.add(o.message);
  222.     Memo1.lines.add('ID Commande : '+o.result.id_commande);
  223.     Memo1.lines.add('Toatal Commande : '+CurrToStr(o.result.total_commande));
  224.     Memo1.lines.add('Client : Code : '+o.result.client.id_client);
  225.     Memo1.lines.add('Client : Nom : '+o.result.client.nom);
  226.     Memo1.lines.add('Client : Ville : Code : '+o.result.client.ville.id_ville);
  227.     Memo1.lines.add('Client : Ville : nom ville : '+o.result.client.ville.nom_ville);
  228.     for s in o.result.payment do
  229.       Memo1.lines.add('Client : Payement  : '+ s);
  230.     for TCollectionItem(pd) in o.result.produits do
  231.      Begin
  232.      Memo1.lines.add('Produit : '+pd.qte.ToString()+' '+pd.nom_produit+' Px: '+CurrToStr(pd.prix));
  233.      Memo1.lines.add('Produit : Ingrédient : '+pd.ingredient.nom_ingredient+' Px: '+CurrToStr(pd.ingredient.prix_ingredient));
  234.      end;
  235.  
  236.   finally
  237.     o.Destroy;
  238.     DeStreamer.Destroy;
  239.   end;
  240. end;
i have message
un message apparait

Code: Text  [Select][+][-]
  1. "unsupported json type for object property  jtArray"


when I add TIngredient to TProduit
lorsque dans le TProduit j'ajoute un type de TIngredient

Thans for help
Merci pour votre aide
Salut,

J'ai essayé de comprendre et d'organiser votre projet, mais il manque certaines parties et ne compile pas.

S'il vous plait, veuillez examiner le petit projet ci-joint et le rendre compilable pour vous aider à mieux.

S'il vous plait, ajoutez des informations initiales afin que les objets soient créés avant leur conversion en JSON.

 

TinyPortal © 2005-2018