Could someone update the teadrinker's function to AHK v2? I tried, but I failed. Thank you in advance.
;Here is my poor attempt:
Code: Select all
#Requires Autohotkey v2.0
#SingleInstance Force
JsonExam := '
(
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
)'
JsonObj := JSON2AHK(JsonExam)
MgBox JsonObj["id"]
JSON2AHK(JSON, Recursive := False) {
Global
Static Doc := ComObject('htmlfile')
, __ := Doc.write('<meta http-equiv="X-UA-Compatible" content="IE=9">')
, JS := Doc.parentWindow
If (!Recursive)
Obj := JSON2AHK(JS.eval('(' . JSON . ')'), True)
Else If !IsObject(JSON)
Obj := JSON
Else If (JSON.toString() != '[object Object]') {
Obj := []
Loop JSON.Length
Obj.Push(JSON2AHK(JSON[A_Index - 1], True) )
}
Else {
Obj := {}
Keys := JS.Object.keys(JSON)
Loop ObjOwnPropCount(Obj) {
K := Keys[A_Index - 1]
Obj[K] := JSON2AHK(JSON[K], True)
}
}
Return Obj
}
And here is the original in AHK v1:
Code: Select all
JsonToAHK(json, rec := false) {
static doc := ComObjCreate("htmlfile")
, __ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
, JS := doc.parentWindow
if !rec
obj := JsonToAHK(JS.eval("(" . json . ")"), true)
else if !IsObject(json)
obj := json
else if json.toString() != "[object Object]" {
obj := []
Loop % json.length
obj.Push( JsonToAHK(json[A_Index - 1], true) )
}
else {
obj := {}
keys := JS.Object.keys(json)
Loop % keys.length {
k := keys[A_Index - 1]
obj[k] := JsonToAHK(json[k], true)
}
}
Return obj
}