Recent

Author Topic: JSONTools Array  (Read 2486 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
JSONTools Array
« on: July 02, 2020, 08:56:54 am »
I have the following array :

TMyArray = array [0..9] of integer;

How do I store and retrieve this array using JSONTools?

I know there is a type nkArray, but I dont know how to use it.

Any ideas / help please?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: JSONTools Array
« Reply #1 on: July 02, 2020, 09:05:21 am »
Code: Pascal  [Select][+][-]
  1.     procedure TForm1.BtnCreateClick(Sender: TObject);
  2.     var  MyArray         : TJSONArray;
  3.           TS              : TStringstream;
  4.      
  5.     function ObjToArray(aName : string; aAge : integer) : TJSONObject;
  6.     begin
  7.      result := TJSONObject.Create;
  8.      result.Add('name', aName);
  9.      result.Add('age', aAge);
  10.     end;
  11.      
  12.     begin
  13.     MyArray := TJSONArray.Create;
  14.      MyArray.Add(ObjToArray('john',50));
  15.      MyArray.Add(ObjToArray('peter',40));
  16.      TS      := TStringstream.Create(MyArray.AsJSON);
  17.      memo1.lines.add(TS.DataString);
  18.     MyArray.free;
  19.     end;
  20.  
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: JSONTools Array
« Reply #2 on: July 02, 2020, 09:18:04 am »
Does that use JSONTools ?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

TRon

  • Hero Member
  • *****
  • Posts: 2503
Re: JSONTools Array
« Reply #3 on: July 02, 2020, 03:22:33 pm »
Does that use JSONTools ?
No, but following code does.

Code: Pascal  [Select][+][-]
  1. program array2json;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. uses
  6.   sysutils, jsontools;
  7.  
  8. type
  9.   TMyArray= array [0..9] of integer;
  10.  
  11. function example1(AArray: TMyArray): TJSonNode;
  12. var
  13.   n: integer;
  14. begin
  15.   // Create a node
  16.   Result:= TJSONNode.Create;
  17.  
  18.   // Make this node an array
  19.   Result.kind:= nkArray;
  20.  
  21.   // Fill, this now, array node. Note that for adding values to an array their name is discarded (hence empty)
  22.   for n in AArray
  23.     do Result.Add('', n);
  24. end;
  25.  
  26. function example2(ANode: TJSonNode; AName: String; AArray: TMyArray): TJSonNode;
  27. var
  28.   JSonArray: TJSonNode;
  29.   n: integer;
  30. begin
  31.   // Adding an array node with provided name
  32.   JSonArray:= ANode.Add(AName, nkArray);
  33.  
  34.   // Fill the array node. Note that for adding values to an array their name is discarded (hence empty)
  35.   for n in AArray
  36.     do JSonArray.Add('', n);
  37.  
  38.   // Return node to caller
  39.   result:= ANode;
  40. end;
  41.  
  42.  
  43. function example3(AArray: TMyArray): TJSonNode;
  44. var
  45.   n: integer;
  46.   i: integer;
  47.   s: string;
  48. begin
  49.   // Create a node
  50.   Result:= TJSONNode.Create;
  51.  
  52.   // Make this node an array
  53.   Result.kind:= nkArray;
  54.  
  55.   // convert the array to a string
  56.   s:= '[';
  57.   for i:= low(AArray) to High(AArray) do
  58.   begin
  59.     s:= s+AArray[i].ToString;
  60.     if i<High(AArray) then s:= s+',';
  61.   end;
  62.   s:= s+']';
  63.  
  64.   // set the value of the array
  65.   Result.Value:= s;
  66. end;
  67.  
  68. var
  69.   MyArray: TMyArray= (10,9,8,7,6,5,4,3,2,1);
  70.   JSon: TJSonNode;
  71.  
  72. begin
  73.   // example 1: returns JSONNode as array
  74.   WriteLn('--- example 1 ---');
  75.   JSon:= example1(MyArray);
  76.   WriteLn(JSon.ToString);
  77.   JSon.Free;
  78.  
  79.   // example 2: add (named) array to (existing) node
  80.   WriteLn('--- example 2 ---');
  81.   JSon:= TJSonNode.Create;
  82.   JSon:= example2(JSon, 'My_Array', MyArray);
  83.   WriteLn(JSon.ToString);
  84.   JSon.Free;
  85.  
  86.   // example 3: return JSonNode as array using text conversion
  87.   JSon:= example3(MyArray);
  88.   WriteLn(JSon.ToString);
  89.   JSon.Free;
  90. end.
  91.  

There are for sure more methods you could use but these seem to be the most obvious ones to me.

edit: stupid typo in code, 3th example was invoking example1, corrected now.
« Last Edit: July 02, 2020, 04:43:37 pm by TRon »

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: JSONTools Array
« Reply #4 on: July 02, 2020, 05:15:56 pm »
Thanks TRon,

How do read the node back to the array?

Thanks.

« Last Edit: July 02, 2020, 05:59:42 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

TRon

  • Hero Member
  • *****
  • Posts: 2503
Re: JSONTools Array
« Reply #5 on: July 02, 2020, 06:45:55 pm »
How do read the node back to the array?
Good that you realised to ask the proper question (e.g. I assume you were not waiting on a "yes" answer)  :P

Sorry for teasing...
Code: Pascal  [Select][+][-]
  1. program json2array;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. uses
  6.   sysutils, jsontools;
  7.  
  8. type
  9.   TMyArray= array [0..9] of integer;
  10.  
  11. function example1(JSon: TjSonNode): TMyArray;
  12. var
  13.   i: integer;
  14.   childnode: TjSonNode;
  15. begin
  16.   // fill array with default values
  17.   result:= default(TMyarray);
  18.  
  19.   // are we dealing with a json array ?
  20.   if JSon.Kind=nkArray then
  21.   begin
  22.     // do the lengths of the arrays match ?
  23.     if JSon.Count=Length(Result) then
  24.     begin
  25.       // init array index
  26.       i:= Low(Result);
  27.       // iterate through json array
  28.       for childNode in JSon do
  29.       begin
  30.         // is this item in the array a number ?
  31.         if childNode.Kind=nkNumber then
  32.         begin
  33.           // pray and hope this child contains an integer
  34.           Result[i]:= childnode.value.ToInteger;
  35.         end
  36.         else WriteLn('json array item is not a number so won''t fit in returning array');
  37.         inc(i);
  38.       end;
  39.     end
  40.     else WriteLn('array does not contain as many items as array does');
  41.   end
  42.   else Result:= default(TMyArray);
  43. end;
  44.  
  45. var
  46.   MyArray: TMyArray;
  47.   JSon: TJSonNode;
  48.   n: integer;
  49. begin
  50.   JSon := TJSonNode.Create;
  51.   try
  52.     JSon.Kind:= nkArray;
  53.     JSon.Value:= ('[10,9,8,7,6,5,4,3,2,1]');
  54.  
  55.     // example1 - convert json array to TMyArray
  56.     WriteLn('--- example 1 ---');
  57.     MyArray:= default(TMyArray);
  58.     Write('before: ');for n in MyArray do Write(n,' ');WriteLn;  
  59.     // provide array node in order to transfer content to MyArray
  60.     MyArray:= example1(JSon);  
  61.     Write('after: ');for n in MyArray do Write(n,' ');WriteLn;
  62.   finally
  63.     JSon.Free;
  64.   end;
  65. end.
  66.  

Please, do realise there are some pitfalls there. I tried to express/address them as many as possible in the code. You would have to address some of these issues inside your own code using your own solution(s), e.g. what to do if not all array items are numbers, what i the sizes do not match and, what to do if a number does not convert properly to an integer etc.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: JSONTools Array
« Reply #6 on: July 03, 2020, 07:50:08 am »
Thanks for your help.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018