handling multiple Json objects through HttpResponse data
pResponse.%JSONImport(tHttpResponse.Data) failing when we have mulitple (array) json objects in response.
set patientSearchList = ##class(msg..patient.PatientSearchList).%New()
s rListObj={}
d rListObj.%Set(ListViewName,[].%FromJSON(tHttpResponse.Data))
do pResponse.%JSONImport(rListObj)
What's the better way to handle multiple json objects coming in response.
Comments
In one of my method, I proceed like this :
Set objJSON = {}
Set objInfo1 = []
Set objInfo2 = []
Populate your Info1 like this :
While(i < X)
{
Set objData = {}
Set objData.name_i = Value_i
// Push Value in info1
do objInfo1 %Push(objData)
}
Add your info1 to object JSON result :
Set objJSON."info1" = objInfo1
Then populate info2
While(i < X)
{
Set objData = {}
Set objData.name_i = Value_i
// Push Value in info2
do objInfo2 %Push(objData)
}
Add your info2 to object JSON result :
Set objJSON."info2" = objInfo2
In your response, return the result :
Set pResponse.result = objJSON.%ToJSON()
I hope this will help u.
Regards
Depends on what are you trying to achieve.
Import as is, with an iterator
Class User.Test Extends (%RegisteredObject, %JSON.Adaptor)
{
Property name As %String;
ClassMethod Import()
{
Set data = [{
"name": "test1"
},
{
"name": "test2"
}]
Set iter = data.%GetIterator()
While iter.%GetNext(.key, .value) {
Set obj = ..%New()
Set tSC = obj.%JSONImport(.value)
Write !,obj.name
}
}
}Import with a wrapper object
Class User.TestList Extends (%RegisteredObject, %JSON.Adaptor)
{
Property items As list Of User.Test;
ClassMethod Import()
{
Set data = [{
"name": "test1"
},
{
"name": "test2"
}]
#; wrap to object
Set data = {
"items": (data)
}
Set list = ..%New()
Set tSC = list.%JSONImport(.data)
For {
set obj = list.items.GetNext(.key)
Quit:key=""
Write !,obj.name
}
}
}