You need to sign in to do that
Don't have an account?
Vidya Bhandary
Multiple resultset into one JSON
I have created an APEX REST class that queries two different objects and returns multiple rows for both queries. Both queries return completely different values.
I want to get the results of both the resultsets into one JSON object which will be automatically parsed due to the REST call.
Is this possible ?
I tried to put both resultsets into a single <sObject> list but it gave me an error saying two different types of objects were being added into a single list.
I want to get the results of both the resultsets into one JSON object which will be automatically parsed due to the REST call.
Is this possible ?
I tried to put both resultsets into a single <sObject> list but it gave me an error saying two different types of objects were being added into a single list.
Try the bleow code snippet as reference:
---------------- Vf page ----------
<apex:page controller="createjson" >
{!jsonobj}
{!objlst}
</apex:page>
--------------- Apex class -----------------
public class createjson
{
public string jsonobj{get;set;}
public list<sObject >objlst{get;set;}
public createjson()
{
list<contact>con=[select id,name from contact limit 1];
jsonobj=json.serializePretty(con);
list<account>acc=[select id,name from account limit 1];
jsonobj=jsonobj+','+json.serializePretty(acc);
objlst=new list<sObject >();
objlst=(list<sObject >)json.deserialize(jsonobj,list<sObject >.class);
}
}
Regards
Ankit Gupta
All Answers
Try the bleow code snippet as reference:
---------------- Vf page ----------
<apex:page controller="createjson" >
{!jsonobj}
{!objlst}
</apex:page>
--------------- Apex class -----------------
public class createjson
{
public string jsonobj{get;set;}
public list<sObject >objlst{get;set;}
public createjson()
{
list<contact>con=[select id,name from contact limit 1];
jsonobj=json.serializePretty(con);
list<account>acc=[select id,name from account limit 1];
jsonobj=jsonobj+','+json.serializePretty(acc);
objlst=new list<sObject >();
objlst=(list<sObject >)json.deserialize(jsonobj,list<sObject >.class);
}
}
Regards
Ankit Gupta
Thank you for your reply.
I tried this way and although I am able to concatenate the resultsets into one - it is not deserializing it properly !!!
It shows only the attributes of the first result set after deserializing.
So
jsonobj = jsonobj + ',' + json.serializePretty(sr_result);
System.debug(jsonobj); ----> This shows all records but it is encoded like this [ {}, {}, {}, {} ],[ {} ]
But you solved the different objects problem; now there is no error on formation of the new sObject list.
objlst = new list<sObject>();
objlst = (list<sObject>)json.deserialize(jsonobj,list<sObject>.class);
System.debug(objlst);
Here I see the list does not have the 2nd results.
I am yet to test if given different set of variables like say 4 vars in result set 1 and 7 vars in result set 2 - it works or breaks but logically it should work !! :)
Tx