I am using code like below
@RestResource(urlMapping='/test/*')
global with sharing class ClassA
{
@HttpPost
global static void createSomething(Wrapper1 req)
{
System.debug('###stringVar1 :' + req.stringVar1);
System.debug('###stringVar2:' + req.stringVar2);
System.debug('###Wrapper2:' + req.Wrapper2);
Object__c obj = new Object__c();
obj.fld1 = req.stringVar1;
obj.fld2 = req.stringVar2;
insert obj;
List<Object2__c> surveyQuestionsRespList = new List<Object2__c>();
for(Wrapper2 question : req.Wrapper2)
{
Object2__c obj2 = new Object2__c();
obj2.fld4 = question.response;
obj2.fld3 = question.someId;
obj2.Object__c = obj.Id;
surveyQuestionsRespList.add(obj2);
}
insert surveyQuestionsRespList;
}
global with sharing class Wrapper1
{
public String stringVar1{get;set;}
public String stringVar2{get;set;}
public List<Wrapper2> Wrapper2{get;set;}
}
global with sharing class Wrapper2
{
public String someId{get;set;}
public String response{get;set;}
}
}
How can I generate JSON for this class. Please help me. Thanks in advance.
Best Answer
You don't need to do anything "special"; just read the classes from front to back, and write the JSON as you go:
You can confirm that you wrote the correct JSON by using json2apex; it should match your original class definitions almost exactly: