You are not logged in.
@ab,
I'm constructing a json object consists array of json objects, like the following:
{
"MyItems":
[
{
"FileName": "111.txt",
"Position": 0
},
{
"FileName": "abc.exe",
"Position": 1
}
]
}
Code for testing, and the 2 questions are written in the comments
procedure TForm4.btnTestAddArrayClick(Sender: TObject);
var
i: Integer;
itemVar: Variant;
arrayVar: Variant;
masterJson: Variant;
begin
TDocVariant.New(arrayVar);
TDocVariant.New(itemVar);
TDocVariant.New(masterJson);
for i := 0 to 1 do
begin
itemVar.FileName := 'file' + IntToStr(i);
itemVar.Position := i;
_Safe(arrayVar).AddItem(itemVar); // is there a better method for adding array item here?
end;
// the following two calls will cause 'Variant method calls not supported.' error, why?
// masterJson.AddOrUpdateValue('ProjectItems', arrayVar);
// masterJson.Value['ProjectItems'] := arrayVar;
_Safe(masterJson).Value['MyArray'] := arrayVar; // ok
ShowMessage(JSONReformat(VariantSaveJSON(masterJson)));
end;
Thanks for your help in advance!
Last edited by edwinsn (2022-07-18 15:17:50)
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
You are making a confusing between variant and TDocVariantData.
AddOrUpdateValue or Value[] are part of TDocVariantData, not exposed as late-binding from the variant variable.
Please read the docs again.
Offline
Oh I see.
masterJson.MyArray := arrayVar;
equals to:
_Safe(masterJson).Value['MyArray'] := arrayVar;
Read the docs again and experimented again, I found that there are actually several ways to add an array of json objects to another json object
Thanks!
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline