UFT11.50 - Service Test - How can I obtain the dat

2019-04-15 02:33发布

问题:

{
    "Id": 456,
    "Type": null,
    "Name": "Store112",
    "Root": "\\\\server\\ShareFolder",
    "Username": "alm",
    "Password": null
}

This is a JSON file response body from a HTTP Request, how can I get the value of 'ID' property? Does UFT or QTP has a function to support this?

回答1:

In order to use a specific JSON property from the response you should do the following.

  1. Select the HTTP tab in the properties pane
  2. In the Response Body section select JSON from the drop down list
  3. Load a sample of the expected JSON response

Then in the activity you want to use the value, click the link icon for the input parameter, select the HTTP tab and select the Id property from the Output section.



回答2:

With QTP10 it is not possible to do this natively, but depending on how much data you want from the JSON file (only ID or do you want to parse the whole thing) and how complicated the JSON file is (multiple childs objects etc.) you can use the following tactics:

  1. Most generic: Try to find a VBScript JSON parser like these guys.
  2. Most hackery: Create an Internet Explorer application instance, attach a (java)script element to the empty document and eval the response body inside this element to a variable. Import this variable to QTP as an object by reading out the DOM. Now you can get the ID by id = ImportedObject.ID
  3. Simplest: Use a regular expression to get the ID with a pattern like ""ID"": *([^,]*) on the response body.

I would opt for 3 unless you want to get much more information from the JSON file.