You need to sign in to do that
Don't have an account?
RarLopz
Debug logs: Wrapper returning null
As seen in debug logs , why is the string json on deserializing returning null ?
{ "caPerformanceRecordList": [ { "motorDealerID": "XL1HX", "dealerName": "Subaru Auto Dealer", "reportingPeriod": "2019-05-01", "financeSourceIntegrationTypeCode": "FIFS", "idlPlus": "Y", "creditApplicationSourceCode": "ECKNWNSPT", "submissionCount": 2 }, { "motorDealerID": "XL1HX", "dealerName": "Subaru Auto Dealer", "reportingPeriod": "2019-05-02", "financeSourceIntegrationTypeCode": "FIFS", "idlPlus": "Y", "creditApplicationSourceCode": "RTEONE", "submissionCount": 15 } ] } public with sharing class CASReportController { public list<JSONWrapperController> wrapper {get;set;} public void deserialize() { string jsonresponse = '{"caPerformanceRecordList":[{"motorDealerID":"XL1HX","dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-01","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"ECKNWNSPT","submissionCount":2 },{"motorDealerID":"XL1HX", "dealerName":"Subaru Auto Dealer", "reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount": 15 },{"motorDealerID": "XL1HX", "dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount":14}]}'; system.debug('json string is ' +jsonresponse); wrapper = (list<JSONWrapperController>) JSON.deserialize(jsonresponse, lis<JSONWrapperController>.class); system.debug('This is the wrapper class: ' +wrapper); } } public class JSONWrapperController { public class CaPerformanceRecordList { public string dealerName {get; set;} public String motorDealerId {get;set;} public DateTime mtd {get; set;} public Integer submissionCount{get;set;} public String idlPlus{get;set;} public String creditApplicationSourceCode{get;set;} } public List<CaPerformanceRecordList> caperfreclist {get;set;} } DEBUG|This is the wrapper class: JSONWrapperController:[caperfreclist=null]
Greetings to you!
- I read your problem and implemented in my Org.
- Your code does not have all the fields which are available in JSON.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.
All Answers
Use below code it will help you:
public class CASReportController {
public class CaPerformanceRecordList {
public String motorDealerID {get;set;}
public String dealerName {get;set;}
public String reportingPeriod {get;set;}
public String financeSourceIntegrationTypeCode {get;set;}
public String idlPlus {get;set;}
public String creditApplicationSourceCode {get;set;}
public Integer submissionCount {get;set;}
public CaPerformanceRecordList(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'motorDealerID') {
motorDealerID = parser.getText();
} else if (text == 'dealerName') {
dealerName = parser.getText();
} else if (text == 'reportingPeriod') {
reportingPeriod = parser.getText();
} else if (text == 'financeSourceIntegrationTypeCode') {
financeSourceIntegrationTypeCode = parser.getText();
} else if (text == 'idlPlus') {
idlPlus = parser.getText();
} else if (text == 'creditApplicationSourceCode') {
creditApplicationSourceCode = parser.getText();
} else if (text == 'submissionCount') {
submissionCount = parser.getIntegerValue();
} else {
System.debug(LoggingLevel.WARN, 'CaPerformanceRecordList consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public List<CaPerformanceRecordList> caPerformanceRecordList {get;set;}
public CASReportController(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'caPerformanceRecordList') {
caPerformanceRecordList = arrayOfCaPerformanceRecordList(parser);
} else {
System.debug(LoggingLevel.WARN, 'CASReportController consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
public static CASReportController parse(String json) {
System.JSONParser parser = System.JSON.createParser(json);
return new CASReportController(parser);
}
public static void consumeObject(System.JSONParser parser) {
Integer depth = 0;
do {
System.JSONToken curr = parser.getCurrentToken();
if (curr == System.JSONToken.START_OBJECT ||
curr == System.JSONToken.START_ARRAY) {
depth++;
} else if (curr == System.JSONToken.END_OBJECT ||
curr == System.JSONToken.END_ARRAY) {
depth--;
}
} while (depth > 0 && parser.nextToken() != null);
}
private static List<CaPerformanceRecordList> arrayOfCaPerformanceRecordList(System.JSONParser p) {
List<CaPerformanceRecordList> res = new List<CaPerformanceRecordList>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new CaPerformanceRecordList(p));
}
return res;
}
}
//Execute the method:
string jsonresponse = '{"caPerformanceRecordList":[{"motorDealerID":"XL1HX","dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-01","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"ECKNWNSPT","submissionCount":2 },{"motorDealerID":"XL1HX", "dealerName":"Subaru Auto Dealer", "reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount": 15 },{"motorDealerID": "XL1HX", "dealerName":"Subaru Auto Dealer","reportingPeriod":"2019-05-02","financeSourceIntegrationTypeCode":"FIFS","idlPlus":"Y","creditApplicationSourceCode":"RTEONE","submissionCount":14}]}';
System.debug('Result : '+CASReportController.parse(jsonresponse));
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Greetings to you!
- I read your problem and implemented in my Org.
- Your code does not have all the fields which are available in JSON.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.
Please follow the below code:
Everything was correct except some simple things that have been highlighted
Hope you find helpful, Please mark as solved.
This is what i finally hav and was able to see the JSON String cast to the JSONWrapperController
Posting my code here for someone who might find it helpful.
I copied this and pasted it as a string to be able to test if it wraps correctly to my JSONWrapperController class . Based on suggestions from above mentioned SFDC community members, this is my classr that that a method which will be eventually called by the Visualforce page
I used the JSON2Apex tool to build the JSONWrappper Class.