如何从JSON文件获取值到变量? [英] How to get value from JSON file to a variable?

查看:838
本文介绍了如何从JSON文件获取值到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I run the protractor test case, I got the below JSON response for the test case result, which get stored in a file as combined.json.

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

Case 1: For passed test case:

{"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:对于失败的测试案例:

Case 2: For Failed test case:

{"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对象中.

In above cases test result is stored in the passed JSON object.

我正在HPALM中编写VAPI测试用例,为此,我需要传递测试用例的状态.我想将passed的值获取到一个变量.

I am writing a VAPI test case in HPALM and for that I need to pass the test case status. I would like to get the value of passed to a variable.

推荐答案

我认为VBScript没有JSON解析器,因此您必须自己解析文件.由于您的情况非常简单(提取特定键的值),因此使用正则表达式应该可以,但是:

I don't think there's a JSON parser for VBScript, so you'll have to parse the file yourself. Since you have a very simple scenario (extract the value for a specific key) using a regular expression should be fine, though:

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

这篇关于如何从JSON文件获取值到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆