Jump to content

Recommended Posts

Posted (edited)

Hello,

I know there are some UDF for JSON but searching on the web i have found one very small in term of line code:

strJson = CreateObject("scripting.filesystemobject").openTextFile("json.txt").readall

Set j = ParseJson(strJson)
wscript.echo j.location.name
wscript.echo j.current.last_updated

'http://demon.tw/my-work/vbs-json.html
Function ParseJson(strJson)
    Set html = CreateObject("htmlfile")
    Set window = html.parentWindow
    window.execScript "var json = " & strJson, "JScript"
    Set ParseJson = window.json
End Function

And i'd like to convert to Autoit. Plus, i hate JSON since i don't understand it.

 I need to know:

1) Check if the value exist before read it

2) How to access to a value like:

"car": {
        "carday": [
            {
                "date": "2016-11-23",
                "date_epoch": 1479859200,
                "day": {
                    "money": 21.3,

I have try with j.car.carday.date but fail

My attempt to convert that code:

Local $strJson = ObjCreate("scripting.filesystemobject")
$strJson.openTextFile("json.txt").readall

Local $j = ParseJson($strJson)
ConsoleWrite($j.location.name)
ConsoleWrite($j.current.last_updated)

Func ParseJson($strJson)
    Local $html = ObjCreate("htmlfile")
    Local $window = $html.parentWindow
    $window.execScript("var json = " & $strJson, "JScript")
    Return $window.json
EndFunc   ;==>ParseJson

Anyway has some draw back to use that? Thanks

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Posted

First try to add COM ERROR HANDLER

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

The error by autoit is:

The requested action with this object has failed for the line with execScript. The COM error is -2147352567

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Posted (edited)

If i have understood isn't better to use the 'autoit version' instead of include the VBS inside the script and launch that? I think in some way is faster, dunno.

EDIT: fail also with eval

$window.eval("var json = " & $strJson)
$window.eval("javascript:var json = " & $strJson)

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Posted (edited)

Assembler lol barely know autoit. At this time the only one that work is the original VBS so i can't make any comparation and still don't understand why on autoit fail. The JSON can be max 2500 lines, i don't know if is "big" is the first time i'll see one.

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Posted
  On 11/23/2016 at 8:55 AM, Terenz said:

The error by autoit is:

The requested action with this object has failed for the line with execScript. The COM error is -2147352567

Expand  

Read HelpFile for:
 

ObjEvent()

Focus on first example especialy how this following function is used:
 

Func _ErrFunc($oError)

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)
Test1.au3 (12) : ==> COM Error intercepted !    
    err.number is:      0x80020009
    err.windescription: 
    err.description is: 80020101.
    err.source is:      
    err.helpfile is:    C:\WINDOWS\system32\mshtml.hlp
    err.helpcontext is:     0
    err.lastdllerror is:    0
    err.scriptline is:  12
    err.retcode is:     0x80020101

Test1.au3 (13) : ==> COM Error intercepted !
    err.number is:      0x80020006
    err.windescription: 
    err.description is: 80020101.
    err.source is:      
    err.helpfile is:    C:\WINDOWS\system32\mshtml.hlp
    err.helpcontext is:     0
    err.lastdllerror is:    0
    err.scriptline is:  13
    err.retcode is:     0x80020101

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Posted

As far as I know 

$strJson.openTextFile("json.txt").readall

ReadAll is parameter not method . Check this on MSDN.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

What? Yes that was the problem! Now works:

Local $hFileOpen = FileOpen(@ScriptDir & "\json.txt")
If $hFileOpen = -1 Then Exit
Local $sFileRead = FileRead($hFileOpen)
FileClose($hFileOpen)

Local $JSON = ParseJson($sFileRead)
ConsoleWrite($JSON.location.name & @CRLF)
ConsoleWrite($JSON.current.last_updated & @CRLF)

Func ParseJson($strJson)
    Local $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
    Local $html = ObjCreate("htmlfile")
    Local $window = $html.parentWindow
    $window.execScript("var json = " & $strJson, "JScript")
    Return $window.json
    #forceref $oError
EndFunc   ;==>ParseJson

Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

I'd like to replace execScript with eval and i still need to understand how to access subtext or whatever is called.

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...