Created
May 12, 2013 14:00
-
-
Save grey-code/5563674 to your computer and use it in GitHub Desktop.
AutoHotkey: JSON_parse()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
derived from Getfree's parseJson function | |
http://www.autohotkey.com/board/topic/93300-what-format-to-store-settings-in/#entry588268 | |
credits to Getfree | |
*/ | |
JSON_parse(jsonStr) { | |
SC := ComObjCreate("ScriptControl") | |
SC.Language := "JScript" | |
ComObjError(false) | |
jsCode = | |
( | |
function arrangeForAhkTraversing(obj) { | |
if(obj instanceof Array) { | |
for(var i=0 ; i<obj.length ; ++i) | |
obj[i] = arrangeForAhkTraversing(obj[i]) ; | |
return ['array',obj] ; | |
} else if(obj instanceof Object) { | |
var keys = [], values = [] ; | |
for(var key in obj) { | |
keys.push(key) ; | |
values.push(arrangeForAhkTraversing(obj[key])) ; | |
} | |
return ['object',[keys,values]] ; | |
} else | |
return [typeof obj,obj] ; | |
} | |
) | |
SC.ExecuteStatement(jsCode "; obj=" jsonStr) | |
return convertJScriptObj2AHK(SC.Eval("arrangeForAhkTraversing(obj)")) | |
} | |
convertJScriptObj2AHK(jsObj) { | |
if (jsObj[0]="object") { | |
obj := {}, keys := jsObj[1][0], values := jsObj[1][1] | |
loop % keys.length | |
obj[keys[A_Index-1]] := convertJScriptObj2AHK(values[A_Index-1]) | |
return obj | |
} else if (jsObj[0]="array") { | |
array := [] | |
loop % jsObj[1].length | |
array.Insert(convertJScriptObj2AHK(jsObj[1][A_Index-1])) | |
return array | |
} else return jsObj[1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment