Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) 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
#include <CkJsonObject.h> #include <CkDateTime.h> #include <CkDtObj.h> void ChilkatSample(void) { CkString strOut; bool success; CkJsonObject json; json.put_EmitCompact(false); // First, let's create JSON containing some date/time strings. json.UpdateString("test.timestamp","2018-01-30T20:35:00Z"); json.UpdateString("test.rfc822","Tue, 24 Apr 2018 08:47:03 -0500"); json.UpdateString("test.dateStrings[0]","2018-01-30T20:35:00Z"); json.UpdateString("test.dateStrings[1]","Tue, 24 Apr 2018 08:47:03 -0500"); json.UpdateNumber("test.StartLoggingTime","1446834998.695"); json.UpdateNumber("test.Expiration","1442877512.0"); json.UpdateInt("test.StartTime",1518867432); strOut.append(json.emit()); strOut.append("\r\n"); // 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. CkDateTime dateTime; CkDtObj dt; bool getAsLocal = false; // Load the date/time at test.timestamp into the dateTime object. success = json.DateOf("test.timestamp",dateTime); strOut.append(dateTime.getAsTimestamp(getAsLocal)); strOut.append("\r\n"); strOut.appendInt(dateTime.GetAsUnixTime(false)); strOut.append("\r\n"); strOut.append(dateTime.getAsRfc822(getAsLocal)); strOut.append("\r\n"); success = json.DateOf("test.rfc822",dateTime); strOut.append(dateTime.getAsTimestamp(getAsLocal)); strOut.append("\r\n"); json.put_I(0); success = json.DateOf("test.dateStrings[i]",dateTime); strOut.append(dateTime.getAsTimestamp(getAsLocal)); strOut.append("\r\n"); json.put_I(1); success = json.DateOf("test.dateStrings[i]",dateTime); strOut.append(dateTime.getAsTimestamp(getAsLocal)); strOut.append("\r\n"); success = json.DateOf("test.StartLoggingTime",dateTime); strOut.append(dateTime.getAsTimestamp(getAsLocal)); strOut.append("\r\n"); success = json.DateOf("test.Expiration",dateTime); strOut.append(dateTime.getAsTimestamp(getAsLocal)); strOut.append("\r\n"); success = json.DateOf("test.StartTime",dateTime); strOut.append(dateTime.getAsTimestamp(getAsLocal)); strOut.append("\r\n"); // 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 = json.DtOf("test.timestamp",getAsLocal,dt); strOut.append("month="); strOut.appendInt(dt.get_Month()); strOut.append(", day="); strOut.appendInt(dt.get_Day()); strOut.append(", year="); strOut.appendInt(dt.get_Year()); strOut.append(", hour="); strOut.appendInt(dt.get_Hour()); strOut.append(", minute="); strOut.appendInt(dt.get_Minute()); strOut.append("\r\n"); success = json.DtOf("test.rfc822",getAsLocal,dt); strOut.append("month="); strOut.appendInt(dt.get_Month()); strOut.append(", day="); strOut.appendInt(dt.get_Day()); strOut.append(", year="); strOut.appendInt(dt.get_Year()); strOut.append(", hour="); strOut.appendInt(dt.get_Hour()); strOut.append(", minute="); strOut.appendInt(dt.get_Minute()); strOut.append("\r\n"); json.put_I(0); success = json.DtOf("test.dateStrings[i]",getAsLocal,dt); strOut.append("month="); strOut.appendInt(dt.get_Month()); strOut.append(", day="); strOut.appendInt(dt.get_Day()); strOut.append(", year="); strOut.appendInt(dt.get_Year()); strOut.append(", hour="); strOut.appendInt(dt.get_Hour()); strOut.append(", minute="); strOut.appendInt(dt.get_Minute()); strOut.append("\r\n"); json.put_I(1); success = json.DtOf("test.dateStrings[i]",getAsLocal,dt); strOut.append("month="); strOut.appendInt(dt.get_Month()); strOut.append(", day="); strOut.appendInt(dt.get_Day()); strOut.append(", year="); strOut.appendInt(dt.get_Year()); strOut.append(", hour="); strOut.appendInt(dt.get_Hour()); strOut.append(", minute="); strOut.appendInt(dt.get_Minute()); strOut.append("\r\n"); success = json.DtOf("test.StartLoggingTime",getAsLocal,dt); strOut.append("month="); strOut.appendInt(dt.get_Month()); strOut.append(", day="); strOut.appendInt(dt.get_Day()); strOut.append(", year="); strOut.appendInt(dt.get_Year()); strOut.append(", hour="); strOut.appendInt(dt.get_Hour()); strOut.append(", minute="); strOut.appendInt(dt.get_Minute()); strOut.append("\r\n"); success = json.DtOf("test.Expiration",getAsLocal,dt); strOut.append("month="); strOut.appendInt(dt.get_Month()); strOut.append(", day="); strOut.appendInt(dt.get_Day()); strOut.append(", year="); strOut.appendInt(dt.get_Year()); strOut.append(", hour="); strOut.appendInt(dt.get_Hour()); strOut.append(", minute="); strOut.appendInt(dt.get_Minute()); strOut.append("\r\n"); success = json.DtOf("test.StartTime",getAsLocal,dt); strOut.append("month="); strOut.appendInt(dt.get_Month()); strOut.append(", day="); strOut.appendInt(dt.get_Day()); strOut.append(", year="); strOut.appendInt(dt.get_Year()); strOut.append(", hour="); strOut.appendInt(dt.get_Hour()); strOut.append(", minute="); strOut.appendInt(dt.get_Minute()); strOut.append("\r\n"); // 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 SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.