#1 2022-12-09 07:45:34

bigheart
Member
Registered: 2014-08-01
Posts: 36

Read Json value to record

Hi,

In mormot2, how to read the json value and assign to record?

type
 TMyValue = packed record
   values: variant;
 end
 TMyValues = array of TMyValue;

 const JsonValues = '[{"values":[1,2,3]}]';

 var
   MyVal: TMyValues;
   DynArr: TDynArray;
 begin
   DynArr.Init(TypeInfo(TMyValues), MyVal);
   DynArr.LoadFromJSON(PUTF8(JsonValues);
  end;

After above function, MyVal is null.

How to load the jason array to variant field?

Last edited by bigheart (2022-12-09 07:46:18)

Offline

#2 2022-12-09 07:59:00

zakaria
Member
Registered: 2021-06-21
Posts: 22

Re: Read Json value to record

You can use TDocVariantData

Offline

#3 2022-12-09 08:23:41

bigheart
Member
Registered: 2014-08-01
Posts: 36

Re: Read Json value to record

The JsonData is from outside.

So i have to use dynamic array.

You mean that  array of double from json value can be stored in the record variant field using TDocVariantData?

Last edited by bigheart (2022-12-09 08:24:44)

Offline

#4 2022-12-09 08:27:07

zakaria
Member
Registered: 2021-06-21
Posts: 22

Re: Read Json value to record

you can use that example:
var doc:TDocVariantData;
doc := TDocVariantData(_Json(jsonDataString));

Offline

#5 2022-12-09 08:47:26

bigheart
Member
Registered: 2014-08-01
Posts: 36

Re: Read Json value to record

Could you give me advise any other way to json array value to set record variant field?

Offline

#6 2022-12-09 11:15:18

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,492
Website

Re: Read Json value to record

If you want the variant to be filled with a TDocVariantData array, you need to set LoadFromJson() with @JSON_[mFast] as CustomVariantOptions parameter.

Offline

Board footer

Powered by FluxBB