从文件中读取对象文字并对其进行评估,而无需在 AutoHotKey 中使用外部库

问题描述 投票:0回答:1

我有一个带有对象文字的文件,如下所示:

[
    {
        name: "ab c",
        windows: [
            {
                one   : 0,
                two: 0,
                three       : 12,
                four    : 6
            },
            {
                one   : 0,
                two: 6,
                three       : 12,
                four    : 6
            }
        ]
    },
    {
        name: "x y z",
        windows: [
            {
                one   : 0,
                two: 0,
                three       : 12,
                four    : 6
            },
            {
                one   : 0,
                two: 6,
                three       : 12,
                four    : 6
            }
        ]
    }

]

我知道我可以将文件作为字符串读取到变量中,如下所示:

data := FileRead(A_ScriptDir "\file.cfg")

我想要做的是将字符串计算为变量,这样它就是我可以在 AHK 2.0 中访问的对象。

所以我应该能够做类似的事情:

MsgBox data[0].name

但我不想使用任何外部库。

这可能吗?

autohotkey
1个回答
0
投票

是的,我们应该将其称为 json,但是,如下所示的一些简单的字符串操作允许使用本机 ahk 语法,而无需 json.ahk 外部库。看看:

Str2 := [{"name":"ab c","windows":[{"one":0,"two":0,"three":12,"four":6},{"one":0,"two":6,"three":12,"four":6}]},{"name":"xyz","windows":[{"one":0,"two":0,"three":12,"four":6},{"one":0,"two":6,"three":12,"four":6}]}]

MsgBox % Str2.1.name . " " . Str2.2.name
MsgBox % Str2.1.windows.1.two . " " . Str2.1.windows.2.two

你应该得到这张照片。

© www.soinside.com 2019 - 2024. All rights reserved.