#1 2017-08-22 22:45:57

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

RecordLoadJSON() returns false

Hello,

Using Delphi 10.2. Target is Win32 executable.
mORMot commit version is 1.18.3599

I have following definitions:

type
  TItem = packed record
    FNo: string;
    FDescription: string;
    FUoM: string;
    FPrice: Double;
    FStock: Double;
  end;
  TItems = TArray<TItem>;

I am receiving below json string from a webservice:

[{"FNo":"LSU-8","FDescription":"8\"100W mellemtone i højttaler","FUoM":"STK","FPrice":21,"FStock":15}]

My code for RecordLoadJSON is as follows:

var
  AItems: TItems;
  Json: WideString;
  AJson: string;
begin
  Response := GetItem(Json, Error);
  if Response <> RESPONSE_OK then
  begin
    ShowMessage('Soap error: ' + Error);
    Exit();
  end;

  AJson := Json;  // Convert WideString to string as I am not able to find where the error is

  if not RecordLoadJSON(AItems, RawUTF8(AJson), TypeInfo(TItems)) then
  begin
    ShowMessage('Json error: Cannot decode json into record. Json string will be printed in Memo');
    Memo1.Lines.Text := AJson;
    Exit();
  end;
end;

Above code always gives me "Cannot decode json..." error displayed for provided json string.

It is very late and I maybe missing something obvious. Just wanted some different eyes to check it out.

I appreciate any help.

Thanks.

-Ertan

Offline

#2 2017-08-23 01:21:48

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: RecordLoadJSON() returns false

You should use DynArrayLoadJSON:

var
  AItems: TItems;
  AJson: RawUTF8;
begin
  AJson := '[{"FNo":"LSU-8","FDescription":"8\"100W mellemtone i højttaler","FUoM":"STK","FPrice":21,"FStock":15}]';
  DynArrayLoadJSON(AItems, @AJson[1], TypeInfo(TItems));

Offline

#3 2017-08-23 08:26:41

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

Re: RecordLoadJSON() returns false

Hello igors233, Thank you. That must be it that I could not see at late night hours. Appreciated.

Offline

Board footer

Powered by FluxBB