问题
{
"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.
- Select the HTTP tab in the properties pane
- In the Response Body section select
JSON
from the drop down list - 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:
- Most generic: Try to find a VBScript JSON parser like these guys.
- 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
- 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.
来源:https://stackoverflow.com/questions/15151600/uft11-50-service-test-how-can-i-obtain-the-data-from-the-response-body