#1 2020-08-15 10:47:45

larand54
Member
Registered: 2018-12-25
Posts: 96

How create a json formatted array of records?

I tried this:

var
  body: variant;
  i: integer;
  files: array of RawUTF8;
  atm: array of variant;
  t: variant;
begin
  setLength(files, 2);
  files[0] := 'C:\temp\Faktura_se.pdf';
  files[1] := 'C:\temp\din.pdf';
  TDocVariant.new(body);
  if high(files) > -1 then
  begin
    SetLength(atm, high(files) + 1);
    for i := 0 to high(files) do
    begin
      TDocVariant.new(atm);
      t.name := extractFileName(files[i]);
      t.value := fileToBase64(files[i]);
      atm[i] := t;
    end;
    body.FileAttachments := _arr([atm]);
  end;

but that just set FileAttachments to '{"FileAttachments":[null]}'

Is there a simple correction of this code?


Delphi-11, WIN10

Offline

#2 2020-08-15 12:15:22

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,258
Website

Re: How create a json formatted array of records?

TDocVariant.new(atm); doesn't mean anything.
I shouldn't compile.
I guess you meant  TDocVariant.new(atm[ i ]);

And no need to use an array of variant.
Just use a main TDocVariant array and TDocVariantData.AddItem() in the loop.

Offline

#3 2020-08-15 13:22:12

larand54
Member
Registered: 2018-12-25
Posts: 96

Re: How create a json formatted array of records?

Thank you, that solved it!


Delphi-11, WIN10

Offline

Board footer

Powered by FluxBB