#1 2019-02-22 18:07:05

ertank
Member
Registered: 2016-03-16
Posts: 163

How to parse such json?

Hello,

Is there a way in mORMot to parse a json like below?

["9af5fdb57d837c02","9af5fdb57dc8e575","9af5fdb57d4fdb61"]

When I check in online tools it resolves as a valid json.

Thanks & regards,
Ertan

Offline

#2 2019-02-22 18:14:34

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

Re: How to parse such json?

You could just use DynArrayLoadJSON() over a TStringDynArray or a TRawUTF8DynArray variable.

Offline

#3 2019-02-22 21:11:05

ertank
Member
Registered: 2016-03-16
Posts: 163

Re: How to parse such json?

Below code did parse above json nicely.

var
  ParsedValues: TStringDynArray;
begin
  if DynArrayLoadJSON(ParsedValues, Pointer(Json), TypeInfo(TStringDynArray)) <> nil then ShowMessage('Parse OK');
end;

Offline

#4 2019-02-22 21:16:24

ertank
Member
Registered: 2016-03-16
Posts: 163

Re: How to parse such json?

Now, I have another challenge with json handling. I just had following json string. That is actually part of a bigger structure.

"v6AssignMode": {
  "6plane": false,
  "rfc4193": false,
  "zt": false
 }

This is last part of that bigger structure and I just stop when I saw that "6plane". My knowledge it is not possible to use a variable name starting with a number. I have to serialize and de-serialize that structure.

I wonder if there is a better solution other than using a StringReplace() and hope not to break anything else in that json?

Thanks & regards,
Ertan

Offline

#5 2019-02-22 22:39:13

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

Re: How to parse such json?

You could use TDocVariantData, and check the fields.

Or you can define your own record and customize the field name used for serialization via TTextWriter.RegisterCustomJSONSerializerFromText().

Offline

#6 2019-02-22 23:09:48

ertank
Member
Registered: 2016-03-16
Posts: 163

Re: How to parse such json?

I tried below using Delphi 10.3.1 and all is good. Both serialization and de-serialization works.

type
  TTest = packed record
    plane: Boolean;
    rfc4193: Boolean;
    zt: Boolean;
  end;

  TMyType = packed record
    v6AssignMode: TTest;
  end;

const
  __TMyType = 'v6AssignMode{ 6plane boolean rfc4193 boolean zt boolean}';

procedure TForm1.Button1Click(Sender: TObject);
var
  Target: string;
  Json: string;
  TestType1: TMyType;
  TestType2: TMyType;
begin
  TestType1.v6AssignMode.plane := True;
  TestType1.v6AssignMode.rfc4193 := False;
  TestType1.v6AssignMode.zt := True;
  Json := RecordSaveJSON(TestType1, TypeInfo(TMyType));
  ShowMessage('Generated: ' + string(Json)); // Here I read JSON with "6plane" in it.

  Target := '{"v6AssignMode":{"6plane":false,"rfc4193":false,"zt":false}}';
  if RecordLoadJSON(TestType2, RawUTF8(Target), TypeInfo(TMyType)) then ShowMessage('OK') else ShowMessage('NOK');  // Here a json string with "6plane" in it correctly de-serialized
end;

Thank you.

Offline

#7 2019-02-22 23:40:26

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

Re: How to parse such json?

You could also write 'v6AssignMode{ 6plane,rfc4193,zt:boolean}' for the definition.

Offline

#8 2019-02-23 17:54:52

ertank
Member
Registered: 2016-03-16
Posts: 163

Re: How to parse such json?

ab wrote:

You could also write 'v6AssignMode{ 6plane,rfc4193,zt:boolean}' for the definition.

That's good to know. It is better for me to shorten as much as possible since main json structure is a big one.

Thanks.

Offline

Board footer

Powered by FluxBB