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)
Sorry for teasing...
program json2array;
{$MODE OBJFPC}{$H+}
uses
sysutils, jsontools;
type
TMyArray= array [0..9] of integer;
function example1(JSon: TjSonNode): TMyArray;
var
i: integer;
childnode: TjSonNode;
begin
// fill array with default values
result:= default(TMyarray);
// are we dealing with a json array ?
if JSon.Kind=nkArray then
begin
// do the lengths of the arrays match ?
if JSon.Count=Length(Result) then
begin
// init array index
i:= Low(Result);
// iterate through json array
for childNode in JSon do
begin
// is this item in the array a number ?
if childNode.Kind=nkNumber then
begin
// pray and hope this child contains an integer
Result[i]:= childnode.value.ToInteger;
end
else WriteLn('json array item is not a number so won''t fit in returning array');
inc(i);
end;
end
else WriteLn('array does not contain as many items as array does');
end
else Result:= default(TMyArray);
end;
var
MyArray: TMyArray;
JSon: TJSonNode;
n: integer;
begin
JSon := TJSonNode.Create;
try
JSon.Kind:= nkArray;
JSon.Value:= ('[10,9,8,7,6,5,4,3,2,1]');
// example1 - convert json array to TMyArray
WriteLn('--- example 1 ---');
MyArray:= default(TMyArray);
Write('before: ');for n in MyArray do Write(n,' ');WriteLn;
// provide array node in order to transfer content to MyArray
MyArray:= example1(JSon);
Write('after: ');for n in MyArray do Write(n,' ');WriteLn;
finally
JSon.Free;
end;
end.
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.