You need to sign in to do that
Don't have an account?
How to convert response in jSON format
Hi Friends,
My class is as below and I need to return the response in JSOn format. My class is returning Accoun t and related contacts. Can any one please help me out?
My class:
*****************
public with sharing class AccountContactList{
public List<account> accountlist{get;set;}
public set<Id> accountids= new set<Id>();
public List<contact> contactlist{get;set;}
//public string eventid{get;set;}
public AccountContactList(){
accountlist= [select Id,Name FROM Account];
for(Account a : accountlist){
accountids.add(a.Id);
}
}
public void getcontactdetails(){
contactlist= [select Id,AccountId,Name From contact WHERE AccountId In:accountids];
system.debug('ContactList:' +contactlist);
}
}
My class is as below and I need to return the response in JSOn format. My class is returning Accoun t and related contacts. Can any one please help me out?
My class:
*****************
public with sharing class AccountContactList{
public List<account> accountlist{get;set;}
public set<Id> accountids= new set<Id>();
public List<contact> contactlist{get;set;}
//public string eventid{get;set;}
public AccountContactList(){
accountlist= [select Id,Name FROM Account];
for(Account a : accountlist){
accountids.add(a.Id);
}
}
public void getcontactdetails(){
contactlist= [select Id,AccountId,Name From contact WHERE AccountId In:accountids];
system.debug('ContactList:' +contactlist);
}
}
You can get the JSON formt using JSON.serialize method.
Please go through the following links
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_JsonGenerator.htm
Please mark as best answer, if the above helps ....!!!!!
SOQL result in JSON (https://salesforcetrail.blogspot.com/2020/05/get-salesforce-soql-result-in-json.html)