Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) JSON Date ParsingDemonstrates how to parse date/time strings from JSON. Note: This example uses the DtOf and DateOf methods introduced in Chilkat v9.5.0.73
IncludeFile "CkDtObj.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkDateTime.pb" Procedure ChilkatExample() success.i json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::setCkEmitCompact(json, 0) ; First, let's create JSON containing some date/time strings. CkJsonObject::ckUpdateString(json,"test.timestamp","2018-01-30T20:35:00Z") CkJsonObject::ckUpdateString(json,"test.rfc822","Tue, 24 Apr 2018 08:47:03 -0500") CkJsonObject::ckUpdateString(json,"test.dateStrings[0]","2018-01-30T20:35:00Z") CkJsonObject::ckUpdateString(json,"test.dateStrings[1]","Tue, 24 Apr 2018 08:47:03 -0500") CkJsonObject::ckUpdateNumber(json,"test.StartLoggingTime","1446834998.695") CkJsonObject::ckUpdateNumber(json,"test.Expiration","1442877512.0") CkJsonObject::ckUpdateInt(json,"test.StartTime",1518867432) Debug CkJsonObject::ckEmit(json) ; We've built the following JSON: ; { ; "test": { ; "timestamp": "2018-01-30T20:35:00Z", ; "rfc822": "Tue, 24 Apr 2018 08:47:03 -0500", ; "dateStrings": [ ; "2018-01-30T20:35:00Z", ; "Tue, 24 Apr 2018 08:47:03 -0500" ; ], ; "StartLoggingTime": 1446834998.695, ; "Expiration": 1442877512.0, ; "StartTime": 1518867432 ; } ; } ; Use the DateOf and DtOf methods to load Chilkat date/time objects with the date/time values. ; The CkDateTime object is primarily for loading a date/time from numerous formats, and then getting ; the date/time in various formats. Thus, it's primarly for date/time format conversion. ; The DtObj object holds a date/time where the individual components (day, month, year, hour, minutes, etc.) are ; immediately accessible as integers. dateTime.i = CkDateTime::ckCreate() If dateTime.i = 0 Debug "Failed to create object." ProcedureReturn EndIf dt.i = CkDtObj::ckCreate() If dt.i = 0 Debug "Failed to create object." ProcedureReturn EndIf getAsLocal.i = 0 ; Load the date/time at test.timestamp into the dateTime object. success = CkJsonObject::ckDateOf(json,"test.timestamp",dateTime) Debug CkDateTime::ckGetAsTimestamp(dateTime,getAsLocal) Debug Str(CkDateTime::ckGetAsUnixTime(dateTime,0)) Debug CkDateTime::ckGetAsRfc822(dateTime,getAsLocal) success = CkJsonObject::ckDateOf(json,"test.rfc822",dateTime) Debug CkDateTime::ckGetAsTimestamp(dateTime,getAsLocal) CkJsonObject::setCkI(json, 0) success = CkJsonObject::ckDateOf(json,"test.dateStrings[i]",dateTime) Debug CkDateTime::ckGetAsTimestamp(dateTime,getAsLocal) CkJsonObject::setCkI(json, 1) success = CkJsonObject::ckDateOf(json,"test.dateStrings[i]",dateTime) Debug CkDateTime::ckGetAsTimestamp(dateTime,getAsLocal) success = CkJsonObject::ckDateOf(json,"test.StartLoggingTime",dateTime) Debug CkDateTime::ckGetAsTimestamp(dateTime,getAsLocal) success = CkJsonObject::ckDateOf(json,"test.Expiration",dateTime) Debug CkDateTime::ckGetAsTimestamp(dateTime,getAsLocal) success = CkJsonObject::ckDateOf(json,"test.StartTime",dateTime) Debug CkDateTime::ckGetAsTimestamp(dateTime,getAsLocal) ; Output so far: ; 2018-01-30T20:35:00Z ; 1517344500 ; Tue, 30 Jan 2018 20:35:00 GMT ; 2018-04-24T13:47:03Z ; 2018-01-30T20:35:00Z ; 2018-04-24T13:47:03Z ; 2015-11-07T00:36:38Z ; 2015-09-22T04:18:32Z ; 2018-02-17T17:37:12Z ; Now load the date/time strings into the dt object: success = CkJsonObject::ckDtOf(json,"test.timestamp",getAsLocal,dt) Debug "month=" + Str(CkDtObj::ckMonth(dt)) + ", day=" + Str(CkDtObj::ckDay(dt)) + ", year=" + Str(CkDtObj::ckYear(dt)) + ", hour=" + Str(CkDtObj::ckHour(dt)) + ", minute=" + Str(CkDtObj::ckMinute(dt)) success = CkJsonObject::ckDtOf(json,"test.rfc822",getAsLocal,dt) Debug "month=" + Str(CkDtObj::ckMonth(dt)) + ", day=" + Str(CkDtObj::ckDay(dt)) + ", year=" + Str(CkDtObj::ckYear(dt)) + ", hour=" + Str(CkDtObj::ckHour(dt)) + ", minute=" + Str(CkDtObj::ckMinute(dt)) CkJsonObject::setCkI(json, 0) success = CkJsonObject::ckDtOf(json,"test.dateStrings[i]",getAsLocal,dt) Debug "month=" + Str(CkDtObj::ckMonth(dt)) + ", day=" + Str(CkDtObj::ckDay(dt)) + ", year=" + Str(CkDtObj::ckYear(dt)) + ", hour=" + Str(CkDtObj::ckHour(dt)) + ", minute=" + Str(CkDtObj::ckMinute(dt)) CkJsonObject::setCkI(json, 1) success = CkJsonObject::ckDtOf(json,"test.dateStrings[i]",getAsLocal,dt) Debug "month=" + Str(CkDtObj::ckMonth(dt)) + ", day=" + Str(CkDtObj::ckDay(dt)) + ", year=" + Str(CkDtObj::ckYear(dt)) + ", hour=" + Str(CkDtObj::ckHour(dt)) + ", minute=" + Str(CkDtObj::ckMinute(dt)) success = CkJsonObject::ckDtOf(json,"test.StartLoggingTime",getAsLocal,dt) Debug "month=" + Str(CkDtObj::ckMonth(dt)) + ", day=" + Str(CkDtObj::ckDay(dt)) + ", year=" + Str(CkDtObj::ckYear(dt)) + ", hour=" + Str(CkDtObj::ckHour(dt)) + ", minute=" + Str(CkDtObj::ckMinute(dt)) success = CkJsonObject::ckDtOf(json,"test.Expiration",getAsLocal,dt) Debug "month=" + Str(CkDtObj::ckMonth(dt)) + ", day=" + Str(CkDtObj::ckDay(dt)) + ", year=" + Str(CkDtObj::ckYear(dt)) + ", hour=" + Str(CkDtObj::ckHour(dt)) + ", minute=" + Str(CkDtObj::ckMinute(dt)) success = CkJsonObject::ckDtOf(json,"test.StartTime",getAsLocal,dt) Debug "month=" + Str(CkDtObj::ckMonth(dt)) + ", day=" + Str(CkDtObj::ckDay(dt)) + ", year=" + Str(CkDtObj::ckYear(dt)) + ", hour=" + Str(CkDtObj::ckHour(dt)) + ", minute=" + Str(CkDtObj::ckMinute(dt)) ; Output: ; month=1, day=30, year=2018, hour=20, minute=35 ; month=4, day=24, year=2018, hour=13, minute=47 ; month=1, day=30, year=2018, hour=20, minute=35 ; month=4, day=24, year=2018, hour=13, minute=47 ; month=11, day=6, year=2015, hour=18, minute=36 ; month=9, day=21, year=2015, hour=23, minute=18 ; month=2, day=17, year=2018, hour=11, minute=37 CkJsonObject::ckDispose(json) CkDateTime::ckDispose(dateTime) CkDtObj::ckDispose(dt) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.