如何从 JSON 文件获取值到变量?

How to get value from JSON file to a variable?

当我 运行 量角器测试用例时,我得到了下面的 JSON 测试用例结果响应,它存储在文件中 combined.json.

案例 1:对于通过的测试案例:

{"description":"tests the case when the login was successful|E2E test Login and search users with different search criteria",
"passed":true,
"os":"XP",
"browser": chrome
}

案例 2:对于失败的测试案例:

{"description":"tests the case when the login was successful|E2E test Login and search users with different search criteria",
"passed":false,
"os":"XP",
"browser": chrome
}

在上述情况下,测试结果存储在 passed JSON 对象中。

我正在 HPALM 中编写 VAPI 测试用例,为此我需要通过测试用例状态。我想将 passed 的值赋给一个变量。

我认为 VBScript 没有 JSON 解析器,因此您必须自己解析文件。由于您有一个非常简单的场景(提取特定键的值),使用正则表达式应该没问题,但是:

Set fso = CreateObject("Scripting.FileSystemObject")

json = fso.OpenTextFile("C:\path\to\combined.json").ReadAll

Set re = New RegExp
re.Pattern = """passed"":(true|false),"
re.IgnoreCase = True

For Each m In re.Execute(json)
  passed = CBool(m.SubMatches(0))
Next