Rav*_*avi 0 xml vbscript json qtp
我正在使用 QTP/UFT 来自动化我的 UI 应用程序。我想将 UI 值与 REST API 响应中的值进行比较。我是 VBScript 新手,我已经编写了调用 REST API 并获取响应的方法,但我正在尝试找到如何使用 VBScript 解析 JSON 的解决方案。
请帮助我如何解析 json 响应?(下面的代码)或者是否更容易接受 xml 中的 REST 响应并在 VBS 中解析它?
感谢您的帮助和想法。谢谢!
userName = "abc@xyz.com"
password = "blah.123"
acctNumber = "01999994201"
URL1="https://CXaic-blah.blah.ocp.blah.com:243/ic/api/integration/v1/flows/rest/blah_ACCNTSEARCH/1.0/accountSearch?accountNumber="
URL=URL1&acctNumber
Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP")
on error resume next
objXmlHttpMain.open "GET",URL, False , userName, password
objXmlHttpMain.setRequestHeader "Accept", "application/json"
objXmlHttpMain.setRequestHeader "charset", "UTF-8"
objXmlHttpMain.send
restjsonresp = objXmlHttpMain.responseText
Run Code Online (Sandbox Code Playgroud)
下面是我得到的 json 响应的格式:
{
"searchResponse":{
"element":[
{
"accType":"R",
"accountNumber":"1111111",
"accountStatus":"A",
"taxId":""
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
虽然我没有 QTP/UFT 来测试或验证以下代码,但我按原样提供这些 JSON 解析解决方案以进行实验......
1) 将 JScript 块注入“htmlfile”对象
Dim y, html : Set html = CreateObject("htmlfile")
Dim window : Set window = html.parentWindow
window.execScript "var json=" & restjsonresp & ";var e=new Enumerator(json.searchResponse.element);", "JScript"
While Not window.e.atEnd()
Set y = window.e.item()
Print "acctType: " & y.accType
Print "accountNumber: " & y.accountNumber
Print "accountStatus: " & y.accountStatus
Print "taxId: " & y.taxId
window.e.moveNext
Wend
Run Code Online (Sandbox Code Playgroud)
2) 使用“MSScriptControl.ScriptControl”调用JScript代码(需要32位)
Dim x, eng : Set eng = CreateObject("MSScriptControl.ScriptControl")
eng.Language = "JScript"
eng.AddCode "function json() { return " & restjsonresp & "; }"
Dim oResp : Set oResp = eng.Run("json")
For Each x In oResp.searchResponse.element
Print "acctType: " & x.accType
Print "accountNumber: " & x.accountNumber
Print "accountStatus: " & x.accountStatus
Print "taxId: " & x.taxId
Next
Run Code Online (Sandbox Code Playgroud)
3) 将 JScript 块注入“InternetExplorer.Application”(过度杀伤?性能下降)
Dim z, objIE : Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate2 "about:blank"
objIE.Toolbar = False
objIE.StatusBar = False
objIE.MenuBar = False
Do While objIE.Busy
Wait 1
Loop
objIE.Visible = False
objIE.document.open "text/html"
objIE.document.write "<script type='text/javascript'>document.json=" & restjsonresp & ";document.jsonEnum = new Enumerator(document.json.searchResponse.element);</script>"
objIE.document.close
While Not objIE.document.jsonEnum.atEnd()
Set z = objIE.document.jsonEnum.item()
Print "acctType: " & z.accType
Print "accountNumber: " & z.accountNumber
Print "accountStatus: " & z.accountStatus
Print "taxId: " & z.taxId
objIE.document.jsonEnum.moveNext
Wend
objIE.Quit
Run Code Online (Sandbox Code Playgroud)
4) 使用 Demon 的 VbsJson 对象(纯 VBScript 解决方案;尽管有更多代码)
https://github.com/eklam/VbsJson
5) 使用正则表达式(仅适用于简单、定义明确的 JSON 响应)
Dim re : Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "\{\s*""searchResponse""\s*\:\s*\{\s*""element""\s*\:\s*\[\s*(\{\s*""accType""\s*\:\s*""(.*)""\s*,\s*""accountNumber""\s*\:\s*""(.*)""\s*,\s*""accountStatus""\s*\:\s*""(.*)""\s*,\s*""taxId""\s*\:\s*""(.*)""\s*\})\s*\]\s*\}\s*\}"
If re.Test(restjsonresp) Then
Dim matches : Set matches = re.Execute(restjsonresp)
Print "acctType: " & matches(0).SubMatches(1)
Print "accountNumber: " & matches(0).SubMatches(2)
Print "accountStatus: " & matches(0).SubMatches(3)
Print "taxId: " & matches(0).SubMatches(4)
End If
Run Code Online (Sandbox Code Playgroud)
6) 将 JSON 转换为 XML,然后解析 XML(大量代码,潜在的杀伤力)
https://github.com/pravynandas/JSONToXML
如果您可以控制响应并提供 XML 而不是 JSON,那么最好在 QTP/UFT 中坚持使用 XML for VBScript。无论如何,我希望这里的内容对您有所帮助。
享受。
| 归档时间: |
|
| 查看次数: |
10719 次 |
| 最近记录: |