Page 1 of 1
JSON - Web API - DATETIME - Conversion
Posted: Thu Sep 08, 2022 1:12 am
by Joerg Hamacher
Hi,
I read a JSON response structure with
Define_Com Class(#PRIM_JSON.Reader) Name(#JsonReader) Textreader(#TextReader)
...
Dowhile Cond(#JsonReader.Read)
...
EndWhile
I have a problem with fields of ValueType DATETIME.
When I look into the body log file of the response it contains Datetime elements like this:
"pickUpDateTime":"2022-09-14T12:00:00.000+00:00"
JSONReader identifies this as ValueType = DATETIME.
When I try to read this field with
- #JsonReader.TokenText.AsNativeString I get the result "14.9.2022 12:0:0:0"
- #JSONReader_TokenText := #JsonReader.TokenText.AsDateTime.AsString the programme crashes
- #JSONReader_TokenText := #JsonReader.TokenAsDateTime.AsString Or #JSONReader_TokenText := #JsonReader.TokenAsDateTime( ISO ).AsString return "00:00:00".
I want to work exactly with the field value "2022-09-14T12:00:00.000+00:00" that I found in the log file.
How can I read the field just the way it was delivered without any conversion?
Kind regards,
Joerg
Re: JSON - Web API - DATETIME - Conversion
Posted: Thu Sep 08, 2022 7:01 am
by Dino
The datetime is actually a string containing a datetime in TZ format, so using this server module (#test97sm) for example:
Code: Select all
Begin_Com Role(*EXTENDS #PRIM_SRVM)
Srvroutine Name(test)
Field_Map For(*Input) Field(#VF_ELTXTX)
Field_Map For(*Output) Field(#std_strng)
Field_Map For(*Output) Field(#datetimex)
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#RAJSONReader) Reference(*Deferred)
Define_Com Class(#XPRIM_ErrorInfo) Name(#ErrorObject) Reference(*Deferred)
#RAJSONReader.SetSourceString String(#VF_ELTXTX) ErrorInfo(#ErrorObject)
* if json not formed correctly
If (*Not #ErrorObject.OK)
Return
Endif
#STD_STRNG := #RAJSONReader.ReadStringWithName( 'pickUpDateTime' ).AsNativeString
#DATETIMEX := #STD_STRNG.AsDateTime( TZ )
Endroutine
End_Com
with this web page shows the datetime is correctly stored:
Code: Select all
Begin_Com Role(*EXTENDS #PRIM_WEB) Theme(#SYS_THEME<MaterialDesignBlue>) Height(624) Width(840)
Define_Com Class(#PRIM_MD.Label) Name(#Text) Caption('Text') DisplayPosition(2) Left(52) Parent(#COM_OWNER) TabPosition(1) Top(36) Height(141) Width(701)
Define_Com Class(#PRIM_MD.Label) Name(#Text1) Caption('Text') DisplayPosition(1) Left(58) Parent(#COM_OWNER) TabPosition(2) ThemeDrawStyle('Strong') Top(150) Width(367)
Evtroutine Handling(#Com_owner.Initialize)
#COM_OWNER.loadjson
Endroutine
Mthroutine Name(loadJson)
Define_Com Class(#test97sm.test) Name(#test)
#test.ExecuteAsync VF_ELTXTX('{"pickUpDateTime":"2022-09-14T12:00:00.000+00:00"}') std_strng(#Text.Caption) datetimex(#datetimex)
Evtroutine Handling(#test.Completed)
#Text1.Caption := #datetimex.AsDisplayString( TZ )
Endroutine
Endroutine
End_Com
Re: JSON - Web API - DATETIME - Conversion
Posted: Thu Sep 08, 2022 5:58 pm
by Joerg Hamacher
Hi Dino,
thank you for this example.
Because of large amount of resonse data (much more than 65535 characters) I have to work with
Define_Com Class(#PRIM_JSON.Reader) Name(#JsonReader) Textreader(#TextReader)
instead of
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#Reader)
I receive the response as a BLOB and transfer this to work with #PRIM_JSON.Reader:
Define_Com Class(#PRIM_IOC.FileStream) Name(#FileStream)
Define_Com Class(#PRIM_IOC.StreamReader) Name(#TextReader) Stream(#FileStream)
Define_Com Class(#PRIM_JSON.Reader) Name(#JsonReader) Textreader(#TextReader)
Define_Com Class(#XPRIM_File) Name(#TempFile)
* Create empty temporary file
#TempFile.CreateTemporaryFile
* Save HTTP response to temporary file
#HTTP_Request.Response.AsFile Autodelete(True) Path(#TempFile)
* Do the reading using JsonReader
#FileStream.Path := #TempFile
And then read JSONReader in a DO WHILE loop.
How can I retrieve the correct DATETIME value using JSONReader here. When I use AsNativeString here (#JsonReader.TokenText.AsNativeString) I get the result "14.9.2022 12:0:0:0" instead of "2022-09-14T12:00:00.000+00:00".
Kind regards,
Joerg