Sample Code :
Dim strPage,strJSON,objIE
strPage = "C:\Jay\JLoader.html"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate2 strPage
While objIE.Busy : Wend
strJSON =
"{""FirstName"":""Jay"",
""LastName"":""Krishna""}"
Set objWin = objIE.document.parentWindow
objWin.execScript "var jsonStr2XML = function(strJSON) {
return json2xml(JSON.parse(strJSON));};"
Msgbox
oWin.jsonStr2XML(strJSON)
objIE.Quit
In Detail:
Converting The most popular data interchange format JSON(JavaScript
Object Notation) to XML using QTP/UFT.
Parsing JSON in UFT could be a challenge so we will use JavaScript
in UFT to make it perfect.
SO We need :
Java Script API - To Convert JSON to XML
JavaScript Files :
JSON parser - To parse
string to generate JSON object.
JSON2.js
parser:
We need a
HTML web page to load all these libraries so We create a JLoader.html file in your desired path like “C:\Jay\JLoader.html “
XHTML
1) Load Webpage:
strPage =
"C:\Jay\JLoader.html"
Set objIE =
CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.Navigate2
strPage
While objIE.Busy :
Wend
2) JSON
String Format:
strJSON =
"{""FirstName"":""Jay"",
""LastName"":""Krishna""}"
3)Convert to
JSON Object:
Set objWin =
objIE.document.parentWindow
objWin.execScript "var jsonStr2XML
= function(strJSON) { return json2xml(JSON.parse(strJSON));};"
4)OUTPUT:
Msgbox
oWin.jsonStr2XML(strJSON)
Comments