简体   繁体   English

使用 VBScript 解析 JSON 文件 - QTP/UFT

[英]Parse JSON file using VBScript - QTP/UFT

I am using QTP/UFT for automating my UI application.我正在使用 QTP/UFT 来自动化我的 UI 应用程序。 I would like to compare the UI values with the values from the REST API response.我想将 UI 值与来自 REST API 响应的值进行比较。 I'm new to VBScript and I have coded the method to call the REST API and get the response but i'm trying to find a solution how to parse the JSON using VBScript.我是 VBScript 的新手,我已经编写了调用 REST API 并获得响应的方法,但我正在尝试找到如何使用 VBScript 解析 JSON 的解决方案。

Please help me how i could parse the json response?请帮助我如何解析 json 响应? (Code below) OR if it's easier to accept the REST response in xml and parse it in VBS? (下面的代码)或者是否更容易接受 xml 中的 REST 响应并在 VBS 中解析它?

Appreciate your help and ideas.感谢您的帮助和想法。 Thanks!谢谢!

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

Below is the format of the json response i get:以下是我得到的 json 响应的格式:

{ 
   "searchResponse":{ 
      "element":[ 
         { 
            "accType":"R",
            "accountNumber":"1111111",
            "accountStatus":"A",
            "taxId":""
         }
      ]
   }
}

While I don't have QTP/UFT to test or verify the following code, I offer-up these JSON parsing solutions as-is for experimentation...虽然我没有 QTP/UFT 来测试或验证以下代码,但我按原样提供了这些 JSON 解析解决方案用于实验......

1) Inject a JScript block into a "htmlfile" object 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

2) Calling JScript code using the "MSScriptControl.ScriptControl" ( requires 32-bit ) 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

3) Injecting a JScript block into "InternetExplorer.Application" ( overkill? perfomance hit ) 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

4) Using Demon's VbsJson object ( a pure VBScript solution; albeit, with more code ) 4) 使用 Demon 的 VbsJson 对象(纯 VBScript 解决方案;尽管有更多代码

https://github.com/eklam/VbsJson https://github.com/eklam/VbsJson

5) Use regular expressions ( only for simple, well-defined JSON responses ) 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

6) Convert JSON to XML, then parse the XML ( lots of code, potential overkill ) 6) 将 JSON 转换为 XML,然后解析 XML(大量代码,潜在的矫枉过正

https://github.com/pravynandas/JSONToXML https://github.com/pravynandas/JSONToXML

If you can control the response, and deliver XML instead of JSON, then it may be preferable to stick with XML for VBScript in QTP/UFT.如果您可以控制响应,并提供 XML 而不是 JSON,那么最好在 QTP/UFT 中坚持使用 XML for VBScript。 Regardless, I hope something here is helpful.无论如何,我希望这里的东西是有帮助的。

Enjoy.享受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM