Forum Discussion
I don't have any experience in C# scripting but if you can interpret this into C# then i hope it will also help you.
I am pretty sure this is not the best way (likely a DLL call to JSON.dll would be better), but if you want it in codes this is what I have in VBScript.
This has limitation, and only does 2 layer parse as below
{
unit id: value
test name: this is the test name
testids
1231321 : fail
23423423 : pass
2423423 : pass
testids2
2423423 : fail
2342342 : pass
2342423 : fail
}
if one of those test id contains another key/pair array, it will also fail (not sure what happen as i never tried)
public function test2()
jsonString="[{""UnitID"":1212133,""testname"":""this is a test"",""test date"":""01/20/2015"",""TestIDs"":[{115525:""pass"",55522522:""fail"",454633:""pass""}],""TestIDs2"":[{115525:""fail"",55522522:""fail"",454633:""pass""}]}]"
jsonString=aqstring.replace(aqstring.replace(jsonstring,"[","",false),"]","",false)
jsonContent=Split(jsonstring,",") 'split based on comma set key/pair - this will fail if your values contains comma - this is just a quick solution for now.
'create the primary holder as the dictionary object
set jsonvalue=CreateObject("Scripting.Dictionary")
for i=0 to ubound(jsonContent)
js=jsonContent(i)
if aqstring.find(js,"{")>-1 then
if jsonvalue.Count=0 then
js=aqstring.Replace(js,"{","",false)
KPValue=Split(js,":")
if ubound(kpvalue) >1 then
kpValue(0)=aqstring.substring(js,0,aqstring.Find(js,":"))
kpvalue(1) =aqstring.substring(js,aqstring.find(js,":"),len(js)-1)
end if
jsonvalue.add aqstring.replace(KPValue(0),chr(34),"",false),KPValue(1)
else
'separate teh { to get teh key testids
tmp=Split(js,"{") 'split to get test id
tmpKey=aqstring.Replace(tmp(0),":","",false) 'contains the key
'i holds the current list counter
set jTmp=CreateObject("Scripting.Dictionary") 'hold the new array key/pair
if aqstring.find(tmp(1),"}")>-1 then 'new array ex. "testIds"":{""ID"":115525}
'1 kp set only
tmpValue=aqstring.Replace(tmp(1),"}","",false)
tmpInnerKP=split(tmpvalue,":")
if ubound(tmpInnerKP) >1 then
tmpInnerKP(0)=aqstring.substring(tmpvalue,0,aqstring.Find(tmpvalue,":"))
tmpInnerKP(1) =aqstring.substring(tmpvalue,aqstring.find(tmpvalue,":"),len(tmpvalue)-1)
end if
jTmp.add aqstring.replace(tmpInnerKP(0),chr(34),"",false),tmpInnerKP(1)
else 'new array ex. "testIds"":{""ID"":115525
'multiple kp set
'process the first key into teh jtmp
tmpInnerKP=split(tmp(1),":")
if ubound(tmpInnerKP)>1 then
tmpInnerKP(0)=aqstring.substring(tmpvalue,0,aqstring.Find(tmpvalue,":"))
tmpInnerKP(1) =aqstring.substring(tmpvalue,aqstring.find(tmpvalue,":"),len(tmpvalue)-1)
end if
jTmp.add aqstring.replace(tmpInnerKP(0),chr(34),"",false),tmpInnerKP(1)
j=i+1 'go to the next item in teh array
do while true
innerJS=jsoncontent(j)
if aqstring.find(innerJS,"}")>-1 then 'new array ex. ""ID"":115525}
'1 kp set only
tmpValue=aqstring.Replace(InnerJS,"}","",false)
tmpInnerKP=split(tmpvalue,":")
if ubound(tmpInnerKP) >1 then
tmpInnerKP(0)=aqstring.substring(tmpvalue,0,aqstring.Find(tmpvalue,":"))
tmpInnerKP(1)=aqstring.substring(tmpvalue,aqstring.find(tmpvalue,":"),len(tmpvalue)-1)
end if
jTmp.add aqstring.replace(tmpInnerKP(0),chr(34),"",false),tmpInnerKP(1)
exit do 'foudn teh closing }, exit it
else
'next set does not contain }, so it is a continuing array list
tmpInnerKP=split(innerJS,":")
if ubound(tmpInnerKP) >1 then
tmpInnerKP(0)=aqstring.substring(tmpvalue,0,aqstring.Find(tmpvalue,":"))
tmpInnerKP(1)=aqstring.substring(tmpvalue,aqstring.find(tmpvalue,":"),len(tmpvalue)-1)
end if
jTmp.Add aqstring.replace(tmpInnerKP(0),chr(34),"",false),tmpInnerKP(1)
j=j+1
end if
loop
i=j
end if
jsonValue.add aqstring.replace(tmpKey,chr(34),"",false),jTmp 'add the key and its dictionary value
end if
else
KPValue=Split(js,":")
if ubound(KPValue) >1 then
KPValue(0)=aqstring.substring(js,0,aqstring.Find(js,":"))
KPValue(1)=aqstring.substring(js,aqstring.find(js,":"),len(js)-1)
end if
jsonvalue.add aqstring.replace(KPValue(0),chr(34),"",false),KPValue(1)
end if
next
'Check to confirm that the second layer (TestIDs2) contain teh faild test id: 115525
'set tmp=Jsonvalue("TestIDs2")
'log.Message tmp("115525")
set test2=jsonvalue
end function
remove the jsonstring above or comment it out
add to the function to accept a paramenter so:
public function test2(jsonString)
....
end function
then if you are using keyword test pass the string to teh function, and it will return the parse version in dictionary object
you can access the value of the the same way as the commented log message :
This will access the testID2s' dictionary object, which you can then access its value based on the key
set tmp=JsonValue("TestIDs2")
log.message tmp("115525")
I'll check this post in case someone post another solution that would be more better that i can also benefit from.
- Colin_McCrae10 years agoCommunity Hero
I'm using C# script with JSON at the moment. But I have my JSON parser in a script extension and I'm using a VBScript one.
It might be easier to use a Jscript one as it provides JSON parsing natively I believe. You'd still need to pop it into a script extension though. (as your project is C# script)
https://msdn.microsoft.com/en-us/library/cc836466(v=vs.84).aspx
- AlexKaras10 years agoCommunity Hero
> You'd still need to pop it into a script extension though. (as your project is C# script)
Actually, C#Script in TestComplete (as well as C++Script) is JScript with the syntax adopted for the C# compiler for Connected applications. This means that JScript code can be used without any changes in C#Script and/or C++Script test projects in TestComplete.