TonyMorgan
Member
Reged: 04/26/11
Posts: 30
|
|
Anyone figure out a good way to parse a JSON formatted file using QTP? Any sample code would be appreciated.
|
AnshooArora
Advanced Member
Reged: 10/27/07
Posts: 612
Loc: New Delhi, India
|
|
Tony: What is a JSON formatted file? Can you post an example?
-------------------- Regards,
Anshoo Arora
[AdvancedQTP] [LinkedIn] [Relevant Codes]
|
TonyMorgan
Member
Reged: 04/26/11
Posts: 30
|
|
here you go:
Code:
{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }
|
AnshooArora
Advanced Member
Reged: 10/27/07
Posts: 612
Loc: New Delhi, India
|
|
Tony: I haven't had a chance to test this, but I did some research and found this:
http://framework.lojcomm.com.br/app/docs/files/app/core/lib/json-class-asp.html
I'm not sure if you have already give it a try, but if you have, please share your findings. Alternatively, I will post something as soon as I get a chance to test this.. which is something I would love to write about.
-------------------- Regards,
Anshoo Arora
[AdvancedQTP] [LinkedIn] [Relevant Codes]
|
TonyMorgan
Member
Reged: 04/26/11
Posts: 30
|
|
Thanks Anshoo. I believe that javascript is necessary to accomplish some of this code. I found the following link:
https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json.asp
That details the functions being called in the link you provided. If you can get it to work, I would appreciate it if you can let me know how it was done.
Thanks,
Tony.
|
Tarun Lalwani
Veteran
Reged: 07/21/05
Posts: 15329
Loc: Milwaukee, Wisconsin
|
|
Well there is a technique of using IE to parse JScript code and then use the same in QTP. If you have the QTP unplugged book then use the same technique discussed in Advanced QTP chapter. If not then let me know and I will post the code from the book here
-------------------- Regards,
Tarun
** First ever technical novel - And I thought I knew QTP! **
** Download QTP Unplugged 2nd Edition eBook for FREE **
KnowledgeInbox RSS
|
TonyMorgan
Member
Reged: 04/26/11
Posts: 30
|
|
No. I don't have the book. If you could post the code, that would be great.
|
Rajkumar_Rajangam
Veteran
Reged: 10/22/10
Posts: 3136
Loc: Norway
|
|
Tony,
Try this...
http://webhole.net/2009/11/28/how-to-read-json-with-javascript/
-------------------- Rajkumar
|
Tarun Lalwani
Veteran
Reged: 07/21/05
Posts: 15329
Loc: Milwaukee, Wisconsin
|
|
Here is a code sample
Code:
'Create a HTML file object Set oHTML = CreateObject("htmlfile") 'Get the object reference to the window object 'this object would allow us to execute JavaScript 'as well as VBScript code inside the host html file Set JS = oHTML.parentWindow 'Execute some code in host application JS.execScript "var JSTest; JSTest = 10;" 'Access the variable declared in the java script 'execute above MsgBox JS.JSTest
-------------------- Regards,
Tarun
** First ever technical novel - And I thought I knew QTP! **
** Download QTP Unplugged 2nd Edition eBook for FREE **
KnowledgeInbox RSS
|
TonyMorgan
Member
Reged: 04/26/11
Posts: 30
|
|
Thanks Tarun. This is allowing me to execute JavaScript but I'm getting a "Could not complete the operation due to error 80020101" error so it appears their is a problem with the javascript code that I need to figure out.
|
Tarun Lalwani
Veteran
Reged: 07/21/05
Posts: 15329
Loc: Milwaukee, Wisconsin
|
|
This error means a error in the script your ran. What I would suggest you is to create a js file and then load it on HTML web page and test the contents in a VBScript section. Use IE for doing this. IE will allow you to use Microsoft Script Debugger to test your JScript code. Below is one good tool for doing so
http://www.ieinspector.com/scriptdebugger/index.html
-------------------- Regards,
Tarun
** First ever technical novel - And I thought I knew QTP! **
** Download QTP Unplugged 2nd Edition eBook for FREE **
KnowledgeInbox RSS
|
TonyMorgan
Member
Reged: 04/26/11
Posts: 30
|
|
Awesome! Thanks everyone for all your help on this.
|
Tarun Lalwani
Veteran
Reged: 07/21/05
Posts: 15329
Loc: Milwaukee, Wisconsin
|
|
So what was the issue exactly?
-------------------- Regards,
Tarun
** First ever technical novel - And I thought I knew QTP! **
** Download QTP Unplugged 2nd Edition eBook for FREE **
KnowledgeInbox RSS
|
TonyMorgan
Member
Reged: 04/26/11
Posts: 30
|
|
I don't know. I stepped through in debugger and it looked fine and it didn't error.
|
kallol
Junior Member
Reged: 06/12/03
Posts: 1
Loc: Gurgaon
|
|
Hi... I am using QTP 11 and using the webservice feature in the tool. I am sending one URI and getting a response back in JSON. How do I parse this JSON to get the name-value or key-value details? I want to parse this in QTP using VB Scripting.
Thanks, Kallol
|
AnshooArora
Advanced Member
Reged: 10/27/07
Posts: 612
Loc: New Delhi, India
|
|
I have tested the following assembly to parse JSON files into XML: http://json.codeplex.com/
You can test against the following sample JSON: http://api.geonames.org/citiesJSON?north...p;username=demo
Code:
Dim lib, url, xmlHttp, json, JsonConvert, xmlDoc
lib = "C:\Bin\Net35\Newtonsoft.Json.dll" url = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"
Set xmlHttp = CreateObject("Microsoft.XMLHTTP") xmlHttp.open "POST", url, False xmlHttp.send
json = "{ ""?xml"": { ""@version"": ""1.0"", ""@standalone"": ""no"" }, ""root"":" & xmlHttp.responseText & "}"
Set JsonConvert = DOTNetFactory.CreateInstance("Newtonsoft.Json.JsonConvert", lib) Set xmlDoc = JsonConvert.DeserializeXmlNode(json)
MsgBox xmlDoc.InnerXml
Note: The following string must be added before the JSON string:
"{ ""?xml"": { ""@version"": ""1.0"", ""@standalone"": ""no"" }, ""root"":"
and the following string after it:
"}"
From the example above:
json = "{ ""?xml"": { ""@version"": ""1.0"", ""@standalone"": ""no"" }, ""root"":" & xmlHttp.responseText & "}"
-------------------- Regards,
Anshoo Arora
[AdvancedQTP] [LinkedIn] [Relevant Codes]
|