You are not logged in.
Pages: 1
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
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
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
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
> 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
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
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
Thank you.
Offline
Pages: 1