dphuc23 Posted July 23, 2021 Posted July 23, 2021 Hello, I'm newbie in AutoIT. I'm trying to get the date and time from a web service which returns the current local time for a given timezone as either plain-text or JSON. Here is the website: http://worldtimeapi.org/api/timezone/Asia/Bangkok JSON: {"abbreviation":"+07","client_ip":"183.81.120.244","datetime":"2021-07-23T14:00:22.225651+07:00","day_of_week":5,"day_of_year":204,"dst":false,"dst_from":null,"dst_offset":0,"dst_until":null,"raw_offset":25200,"timezone":"Asia/Bangkok","unixtime":1627023622,"utc_datetime":"2021-07-23T07:00:22.225651+00:00","utc_offset":"+07:00","week_number":29} Here is what I found Local $String = BinaryToString(INetRead ("http://worldtimeapi.org/api/timezone/Asia/Bangkok",1)) $Obj = Json_Decode($String) $datetime = functionOfMyDreams($Obj, "" ,"datetime") ConsoleWrite($datetime) Func functionOfMyDreams($decodedObject, $targetObject, $targetKeyInObject) $value = Json_Get($decodedObject, '["' & $targetObject & '"]["' & $targetKeyInObject & '"]') return $value EndFunc ;==>functionOfMyDreams The UDF: https://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/
FrancescoDiMuro Posted July 23, 2021 Posted July 23, 2021 @dphuc23 With a different approach, this would be helpful: #include <Array.au3> #include <StringConstants.au3> #include <MsgBoxConstants.au3> MsgBox($MB_ICONINFORMATION, "GetDateTimeFromWorldTimeAPI", GetDateTimeFromWorldTimeAPI("http://worldtimeapi.org/api/timezone/Asia/Bangkok")) _ArrayDisplay(GetDateTimeArrayFromWorldTimeAPI("http://worldtimeapi.org/api/timezone/Asia/Bangkok"), "GetDateTimeArrayFromWorldTimeAPI") Func GetDateTimeFromWorldTimeAPI($strAddress) Return StringRegExp(BinaryToString(INetRead($strAddress,1)), '"datetime":"([^"]+)"', $STR_REGEXPARRAYMATCH)[0] EndFunc Func GetDateTimeArrayFromWorldTimeAPI($strAddress) Return StringRegExp(BinaryToString(INetRead($strAddress,1)), '"datetime":"(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2}\.\d+)([+-]\d+:\d+)"', $STR_REGEXPARRAYMATCH) EndFunc Cheers Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
dphuc23 Posted July 23, 2021 Author Posted July 23, 2021 1 minute ago, FrancescoDiMuro said: @dphuc23 With a different approach, this would be helpful: #include <Array.au3> #include <StringConstants.au3> #include <MsgBoxConstants.au3> MsgBox($MB_ICONINFORMATION, "GetDateTimeFromWorldTimeAPI", GetDateTimeFromWorldTimeAPI("http://worldtimeapi.org/api/timezone/Asia/Bangkok")) _ArrayDisplay(GetDateTimeArrayFromWorldTimeAPI("http://worldtimeapi.org/api/timezone/Asia/Bangkok"), "GetDateTimeArrayFromWorldTimeAPI") Func GetDateTimeFromWorldTimeAPI($strAddress) Return StringRegExp(BinaryToString(INetRead($strAddress,1)), '"datetime":"([^"]+)"', $STR_REGEXPARRAYMATCH)[0] EndFunc Func GetDateTimeArrayFromWorldTimeAPI($strAddress) Return StringRegExp(BinaryToString(INetRead($strAddress,1)), '"datetime":"(\d{4}\-\d{2}\-\d{2})T(\d{2}:\d{2}:\d{2}\.\d+)([+-]\d+:\d+)"', $STR_REGEXPARRAYMATCH) EndFunc Cheers Thanks
Danp2 Posted July 23, 2021 Posted July 23, 2021 (edited) 5 hours ago, dphuc23 said: The UDF: https://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/ The one I've been using is this one, which includes a handy function named json_dump. This function lists the keys and their values to the console. This -- Local $String = BinaryToString(INetRead ("http://worldtimeapi.org/api/timezone/Asia/Bangkok",1)) json_dump($String) gives this output -- +-> .abbreviation =+07 +-> .client_ip =68.1.xx.xx +-> .datetime =2021-07-23T19:29:33.452612+07:00 +-> .day_of_week =5 +-> .day_of_year =204 +-> .dst =False +-> .dst_from =Null +-> .dst_offset =0 +-> .dst_until =Null +-> .raw_offset =25200 +-> .timezone =Asia/Bangkok +-> .unixtime =1627043373 +-> .utc_datetime =2021-07-23T12:29:33.452612+00:00 +-> .utc_offset =+07:00 +-> .week_number =29 So using this UDF, your code would look like this -- Local $String = BinaryToString(INetRead ("http://worldtimeapi.org/api/timezone/Asia/Bangkok",1)) $Obj = Json_Decode($String) $datetime = functionOfMyDreams($Obj, "datetime") ConsoleWrite($datetime & @CRLF) Func functionOfMyDreams($decodedObject, $targetKeyInObject) $value = Json_Get($decodedObject, '["' & $targetKeyInObject & '"]') return $value EndFunc ;==>functionOfMyDreams Edited July 23, 2021 by Danp2 Latest Webdriver UDF Release Webdriver Wiki FAQs
CarlArnold Posted May 25, 2023 Posted May 25, 2023 (edited) Hello! It looks like you're trying to retrieve the date and time from a web service that returns the current local time for a given timezone in either plain text or JSON format using AutoIt. Have you tried the LinkedIn Sales Navigator extension to relieve the process? I see that you have already made some progress, and you are using the JSON UDF library to handle the JSON data. Based on the code you provided, it seems like you're on the right track. However, there are a couple of modifications you need to make. Here's an updated version of your code: #include <JSON.au3> Local $String = BinaryToString(InetRead("http://worldtimeapi.org/api/timezone/Asia/Bangkok", 1)) Local $Obj = _JSON_Decode($String) Local $datetime = functionOfMyDreams($Obj, "", "datetime") ConsoleWrite($datetime) Func functionOfMyDreams($decodedObject, $targetObject, $targetKeyInObject) Local $value = _JSON_Get($decodedObject, $targetObject & "|" & $targetKeyInObject) Return $value EndFunc ;==>functionOfMyDreams In this updated code: I added the #include <JSON.au3> line at the beginning to include the JSON UDF library. I replaced the Json_Decode function with _JSON_Decode, which is the correct function name from the JSON UDF library. I modified the Json_Get function to _JSON_Get to use the correct function from the JSON UDF library. Additionally, I changed the way the target object and key are passed to the function. Instead of using double quotes and concatenation, I used the pipe character (|) to separate the object and key. This is the expected format for the _JSON_Get function. With these modifications, the code should be able to retrieve the datetime value from the JSON response and print it using ConsoleWrite. Make sure you have the JSON UDF library included in your script directory or use the full path to the UDF file if it's located elsewhere. Edited May 26, 2023 by CarlArnold
Andreik Posted May 25, 2023 Posted May 25, 2023 Grave digging. The OP has not been online in the last 2 years.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now