You are not logged in.
Pages: 1
type
TRec = packed record
firstname: string;
end;
procedure Test;
var
r: TRec;
ar: TArray<TRec>;
x: RawUtf8;
begin
r.firstname := 'sdf';
ar := ar + [r];
DynArrayLoadJSON(ar, UniqueRawUTF8(x), TypeInfo(TArray<TRec>)); -> access violation
An error occured in line:
Ctxt.Json := GotoNextNotSpace(Ctxt.Json); -> Ctxt.Json is nil
in method:
_JL_DynArray
Offline
I'm using 10.4.
I want to convert json into array of TRec structure, something like TJson.JsonToObject<TPerson>(Memo1.Lines.Text);
Offline
var
x: UTF8String;
r: TRec;
ar: TArray<TRec>;
begin
r.firstname := 'ssss';
ar := ar + [r];
x := '[]';
DynArrayLoadJSON(ar, UniqueRawUTF8(x), TypeInfo(TArray<TRec>));
ShowMessage(x);
it does not raise AV but the x does not contain any data, just empty array as initialized
Offline
Try it as follows:
type
TRec = packed record
firstName: String;
end;
TRecDynArray = array of TRec;
var
json: RawJson;
recs: TRecDynArray;
begin
json := '[{"firstName": "one"}, {"firstName": "two"}, {"firstName": "three"}]';
if DynArrayLoadJson(recs, json, TypeInfo(TRecDynArray)) then
ShowMessage(recs[1].firstName);
Or if I read your post again, you may need the following:
var
recs: TRecDynArray;
begin
SetLength(recs, 3);
recs[0].firstName := 'one';
recs[1].firstName := 'two';
recs[2].firstName := 'three';
ShowMessage(Utf8ToString(DynArraySaveJson(recs, TypeInfo(TRecDynArray))));
With best regards
Thomas
Last edited by tbo (2022-09-04 21:43:08)
Offline
Thank you, the:
ShowMessage(Utf8ToString(DynArraySaveJson(ar, TypeInfo(TArray<TRec>))));
also is OK.
Offline
Pages: 1