wie bereits angedroht, hier nun die erste Ausgabe der InJSON()-function, zart inspiriert von AHK's InStr().
Tatsächlich handelt es sich um eine angepimpte Version eines scripts von @Descolada nachdem ich vorab hier support dazu angefragt hatte.
Die "Insider Story" dazu: Click()
Bei geringen Mengen Code lässt sich JSON nach "Beautifying" (siehe Anhang im Code) ja noch gemütlich entwirren.
Doch größere Mengen "Bandwurm-JSON" machen das schon nervend.
Genau. Deshalb dies.
Kriminalforensiker beim Besuch des AHK-Forums: "Ach sieh mal da - eine Code-Anhaftung!"
Code: Select all
#SingleInstance force
SetBatchLines,-1
#Include <JSON> ; https://raw.githubusercontent.com/cocobelgica/AutoHotkey-JSON/master/JSON.ahk
str={"widget": {"debug": "on","window": {"title": "Sample Konfabulator Widget","name": "main_window","width": 500,"height": 500},"image": {"src": "Images/Sun.png","name": "sun1","hOffset": 250,"vOffset": 250,"alignment": "center"},"text": {"data": "Click Here","size": 36,"style": "bold","name": "text1","hOffset": 250,"voffset": 100,"alignment": "center","onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"}}}
global path :=[], paths:="" ; 'path'-array zur definierten key-suche bzw. 'paths'-variable zur ausgabe der pfade aller keys
obj := JSON.Load(str) ; laden des JSON-strings in ein object
InJSON(obj, "vOffset") ; suche nach dem schlüssel "vOffset" ohne case-sensitive parameter (default)
MsgBox % path.Count() ; gesamtanzahl gefundener schlüssel. Ergebnis: 1
MsgBox % path.1 ; ausgabe des ersten gefundenen schlüssels: Ergebnis: 'widget.image.vOffset [250]'
MsgBox % path[path.Count()] ; ausgabe des letzten gefundenen schlüssels. Ergebnis: 'widgte.image.vOffset [250]'
path:=[] ; zurücksetzen des 'path'-array
InJSON(obj, "voffset", false) ; suche nach dem schlüssel "voffset" per case-sensitive parameter: 'false' bzw. 'true'
MsgBox % path.Count() ; gesamtanzahl gefundener schlüssel. Ergebnis: 2
MsgBox % path.1 ; ausgabe des ersten gefundenen schlüssels: Ergebnis: 'widget.image.vOffset [250]'
MsgBox % path[path.Count()] ; ausgabe des letzten gefundenen schlüssels: Ergebnis: 'widget.text.voffset [100]'
paths:="" ; zurücksetzen der 'paths'-variable
InJSON(obj, "")
MsgBox % paths ; aufschlüsselung/ausgabe aller pfade inklusive deren werte
InJSON(obj, key, case=true, delim=".", curPath="") {
if !IsObject(obj)
return
for k, v in obj {
op:=(case)?"==":"="
paths .= (curPath=="")?k "`n":(curPath delim k) " [" v "]`n"
if opSwap(k,op,key)
path.Push(curPath==""?k:(curPath delim k) " [" v "]")
if f := InJSON(v, key, case, delim, (curPath=="")?k:(curPath delim k))
Return f
}
}
opSwap(before, operator, after) { ; //www.autohotkey.com/board/topic/52059-if-variable1-operator-variable2-dynamic-expression/?p=325776
Return, operator = "=" ? (before = after)
: operator = "==" ? (before == after)
: operator = ">" ? (before > after)
: operator = ">=" ? (before >= after)
: operator = "<" ? (before < after)
: operator = "<=" ? (before <= after)
: operator = "<>" ? (before <> after)
: operator = "!=" ? (before != after)
: "ERROR"
}
/*
str = ; https://www.json.org/example.html
(LTrim
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"voffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
)
*/
HTH
Bo
PS.
Ich vermute, dass sich das script durchaus an murksigem JSON verschlucken kann. Falls also noch RegEx-injection oder ähnliches Kryptonit droht - keine schamhafte Zurückhaltung.
Ich halte das aus