#1 2016-12-30 10:04:25

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

Help on deserialization of a json

Hello,

I am using 1.18.3101 version of Synopse with Delphi 10.1 Update 2. Target is 32bit EXE.

I am provided a rest web service with following json returning as a reply
http://pasted.co/de92e9db

My types defined for it in Delphi as to service documentation is as follows:
http://pasted.co/051c3880

I try to deserialize using following code. Please note that I am still trying to understand json basics. So, below code might completely be wrong.

procedure TForm1.Button1Click(Sender: TObject);
var
  RequestDetails: TRequest;
  Response: TResponse;
  lRequest: TStringStream;
  JsonString: RawUTF8;
  ResponseJson: string;
begin
  // Request information is filled correctly. Request json is prepared below
  JsonString := RecordSaveJSON(RequestDetails, TypeInfo(TRequest));
  lRequest := TStringStream.Create(UTF8ToString(JsonString), TEncoding.UTF8);
  try
    Screen.Cursor := crHourGlass;
    Memo1.Lines.Add('service link: ' + ServiceURL);
    Memo1.Lines.Add('request time: ' + DateTimeToStr(Now()));
    Memo1.Lines.Add(UTF8ToString(JsonString));
    IdHTTP1.Request.ContentType := 'application/json';
    IdHTTP1.Request.CharSet     := 'utf-8';
    try
      // Get response
      ResponseJson := IdHTTP1.Post(ServiceURL, lRequest);
      Memo1.Lines.Add('incoming: ' + DateTimeToStr(Now()));
      Memo1.Lines.Add(ResponseJson);
      RecordLoadJSON(Response, RawUTF8(ResponseJson), TypeInfo(TResponse));
      Memo1.Lines.Add('Response: ' + BoolToStr(Response.bReturnValue, True));
      Memo1.Lines.Add('Total Receipt(s) in Data: ' + Length(Response.xExtReceipts).ToString()); // Always zero here.
    except
      on E: Exception do
      begin
        ShowMessage('Error on request: ' + sLineBreak + E.Message);
      end;
    end;
  finally
    lRequest.Free();
    Screen.Cursor := crDefault;
  end;
end;

My problem is RecordLoadJson() always returns false and I cannot understand what problem is in my case.

Any help is appreciated.

Thanks & regards,
Ertan

Offline

#2 2016-12-30 13:27:35

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

Re: Help on deserialization of a json

After spending some hours, I found that documentation is not showing reality. There are just a bit different field names are used in the incoming Json. After adapting name changes in my code everything is fine now.

Thanks.

Offline

#3 2016-12-30 19:44:51

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

Re: Help on deserialization of a json

For quick parsing and accessing JSON values you can also use TDocVariantData, for example:

var
  v: Variant;
begin
  v := _JsonFast('{"Field1":10}');
  ShowMessage(v.Field1);
end;

Offline

#4 2016-12-30 20:18:51

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

Re: Help on deserialization of a json

Thanks igors233.

I tried to test this method with my json which has some nested levels. Unfortunately, I could not manage this. Would you give me a sample to display "xlZReportData[0].xZReportCashierDataList[0].iCashierName" as a message?

Thanks.

Offline

#5 2016-12-31 09:27:22

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

Re: Help on deserialization of a json

> I tried to test this method with my json which has some nested levels. Unfortunately, I could not manage this. Would you give me
> a sample to display "xlZReportData[0].xZReportCashierDataList[0].iCashierName" as a message?

It's hard to say like this, can you send example of your json content so I could try it?

Offline

#6 2016-12-31 09:32:17

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

Re: Help on deserialization of a json

Records and dynamic arrays are just fine for complex objects of fixed structure.
TDocVariantData is great is your objects/arrays structure may vary.

Offline

#7 2016-12-31 19:22:01

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

Re: Help on deserialization of a json

igors233 wrote:

It's hard to say like this, can you send example of your json content so I could try it?

Sample Json is in a link in my initial post.

Offline

#8 2017-01-01 18:59:05

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

Re: Help on deserialization of a json

ertank wrote:

I tried to test this method with my json which has some nested levels. Unfortunately, I could not manage this. Would you give me a sample to display "xlZReportData[0].xZReportCashierDataList[0].iCashierName" as a message?

You could write:
  v := _JsonFast(WinAnsiToUtf8(json));
  ShowMessage(v.xlZReportData._(0).xZReportCashierDataList._(0).iCashierName);

Offline

#9 2017-01-01 19:18:35

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

Re: Help on deserialization of a json

Thank you.

Offline

Board footer

Powered by FluxBB