#1 2018-11-17 09:40:18

DKA
Member
Registered: 2016-07-05
Posts: 39

Modifying JSON

Hi,

I'm playing with JSON and I face a problem:
Here is a relevant piece of code.

....
  sFeatures:= _Json(MemJSON.Lines.Text)  ;                                                                 //Loading text from TMemo
  vFeatures := _JsonFast(sFeatures);                                                                             //vFeatures is Variant   
...
     vFeatures.Features._(0).properties.Id := 1111;                                                        //works well. I can change the value of Id
     sLat:=  vFeatures.Features._(0).Geometry.Coordinates._(0)._(0)._(0);                  //Works well. I can read the value and sLat is RawUTF8.
     vFeatures.Features._(0).Geometry.Coordinates._(0)._(0)._(0) := sLat;                   //EVariantDispatchError. I cannot copy back the same value
...
    
Obviously, I missed something in the documentation. Some ideas?

Thanks

Last edited by DKA (2018-11-23 20:21:45)

Offline

#2 2018-11-23 20:08:23

DKA
Member
Registered: 2016-07-05
Posts: 39

Re: Modifying JSON

I fixed the problem using TDocVariant like this


var
     v: Variant;
     vFeatures, vGeometry, vCoordinatesAll, vCoordinates,vLatLong: TDocVariantData;
     sFeatures,sTheValue : RawUtf8;

begin
  // Load from Memo
  sFeatures:= _Json(MemJSON.Lines.Text)  ;
  v := _JsonFast(sFeatures);

  vFeatures := _Safe(v.Features)^;
  vGeometry := _Safe(vFeatures.Values[0].Geometry)^;
  vCoordinatesAll := _Safe(vGeometry.Values[0])^;
  vCoordinates := _Safe(vCoordinatesAll.Values[0])^;
  vLatLong := _Safe(vCoordinates.Values[0])^;


  vLatLong.Values[0] := '12'; //this works

Offline

#3 2018-11-25 20:17:19

DKA
Member
Registered: 2016-07-05
Posts: 39

Re: Modifying JSON

Hi again.
Here is a piece of code.

procedure TForm1.Button1Click(Sender: TObject);
var iNbFeatures,j,iNbProperties :integer;
    vFeatures, vProperties: TDocVariantData;
    v:Variant;
begin
  v := _JsonFast('{"features":[{"properties":{"Name":"blabla","Id":11}}]}');
  vFeatures := _Safe(v.Features)^;
  iNbFeatures:= vFeatures.Count;
  for j:=0 to iNbFeatures-1 do
  begin
    vProperties := _Safe(vFeatures.Values[j].properties)^;
    iNbProperties := vProperties.Count;  //Here iNbProperties = 2 .
    vProperties.Values[1]:= 'NEW VALUE FOR P1';  //Property.value is modified in the debbuger
    vProperties.Names[0]:= 'NEW NAME FOTR P0';   //Property.name is modified in the debbuger
    vProperties.AddValue('Level', 3);
    iNbProperties := vProperties.Count;  //Here iNbProperties = 3.
    vProperties.AddNameValuesToObject(['RRRR', 'RRRRR']);
    iNbProperties := vProperties.Count;  //Here iNbProperties = 4.
  end;
   {
   But Here,
   v._JSON shows only initial properties modified}
end;

New value of v is :
'{"features":[{"properties":{"NEW NAME FOTR P0":"blabla","Id":"NEW VALUE FOR P1"}}]}'
Only initial properties were modified. I can add property into vProperties but there is no propagation to vFeatures.

Why?
Thanks

Last edited by DKA (2018-11-25 20:17:38)

Offline

#4 2018-11-25 20:19:23

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

Re: Modifying JSON

You use TDocVariantData local variables, so the values are copied into those, and therefore modifications not propagated.

Use pointers, i.e. the PDocVariantData as returned by _Safe() to modify an existing instance.

Offline

#5 2018-11-27 21:12:20

DKA
Member
Registered: 2016-07-05
Posts: 39

Re: Modifying JSON

Thanks ab. Changing to PDocVariantData fix the problem.

Offline

Board footer

Powered by FluxBB