What do you mean?Tre4shunter wrote: ↑completely wrong number
I see "00540".
Code: Select all
jsonobj := {"Key 1":044505,"Key 2":"00540"}
MsgBox, % "Key 1 as interpreted by AHK: " . jsonobj["Key 1"] . "`n" ; leading zeros are removed by AHK
. "Key 2: " . jsonobj["Key 2"]
jsonstring := LightJson.Stringify(jsonobj)
msgbox % jsonstring ; the same values as in the first MsgBox
class LightJson
{
static JS := LightJson.GetJS(), true := {}, false := {}, null := {}
Parse(json, _rec := false) {
if !_rec
obj := this.Parse(this.JS.JSON.parse(json), true)
else if !IsObject(json)
obj := json
else if this.JS.Object.prototype.toString.call(json) == "[object Array]" {
obj := []
Loop % json.length
obj.Push( this.Parse(json[A_Index - 1], true) )
}
else {
obj := {}
keys := this.JS.Object.keys(json)
Loop % keys.length {
k := keys[A_Index - 1]
obj[k] := this.Parse(json[k], true)
}
}
Return obj
}
Stringify(obj, indent := "") {
if indent|1 {
for k, v in ["true", "false", "null"]
if (obj = this[v])
Return v
if IsObject( obj ) {
isArray := true
for key in obj {
if IsObject(key)
throw Exception("Invalid key")
if !( key = A_Index || isArray := false )
break
}
for k, v in obj
str .= ( A_Index = 1 ? "" : "," ) . ( isArray ? "" : """" . k . """:" ) . this.Stringify(v, true)
Return str = "" ? "{}" : isArray ? "[" . str . "]" : "{" . str . "}"
}
else if !(obj*1 = "" || RegExMatch(obj, "^-?0|\s"))
Return obj
for k, v in [["\", "\\"], [A_Tab, "\t"], ["""", "\"""], ["/", "\/"], ["`n", "\n"], ["`r", "\r"], [Chr(12), "\f"], [Chr(8), "\b"]]
obj := StrReplace( obj, v[1], v[2] )
Return """" obj """"
}
sObj := this.Stringify(obj, true)
Return this.JS.eval("JSON.stringify(" . sObj . ",'','" . indent . "')")
}
GetJS() {
static Doc, JS
if !Doc {
Doc := ComObjCreate("htmlfile")
Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
JS := Doc.parentWindow
( Doc.documentMode < 9 && JS.execScript() )
}
Return JS
}
}