0

JSON Response:

{
    "caPerformanceRecordList": [

    {
        "motorDealerID": "XL1HX",
        "dealerName": "Subaru Auto Dealer",
        "reportingPeriod": "2019-05-18",
        "financeSourceIntegrationTypeCode": "FIFS",
        "idlPlus": "Y",
        "creditApplicationSourceCode": "STEONE",
        "submissionCount": 2
    },
    {
        "motorDealerID": "XL1HX",
        "dealerName": "Subaru Auto Dealer",
        "reportingPeriod": "2019-06-27",
        "financeSourceIntegrationTypeCode": "FIFS",
        "idlPlus": "Y",
        "creditApplicationSourceCode": "STEONE",
        "submissionCount": 5
    }
],
    "ecPerformanceRecordList": [
    {
        "motorDealerID": "XL1HX",
        "submitUserID": "BCUSER1",
        "bookedDate": "2019-05-22 00:00:00.0",
        "ecEligible": "Y",
        "caBookedCount": 1,
        "ecBookedCount": 0
    },
    {
        "motorDealerID": "XL1HX",
        "submitUserID": "BCUSER1",
        "bookedDate": "2019-06-05 00:00:00.0",
        "ecEligible": "Y",
        "caBookedCount": 1,
        "ecBookedCount": 0
    }
   ]
}




    public with sharing class CASReportController {

        public list<JSONWrapperController> wrapper {get;set;}

        public list<JSONWrapperController> 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, 
     list<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;} 

  }
0

3 Answers 3

0

It appears that your JSON is dirty:

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}]}';

Above is what you have - but it appears that you forgot the : in your JSON Object after the first key.

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}]}';

Give this a try, it may resolve your issue with the bad JSON String.

also it appears that you are casting the wrong type here. Should be a Map<String, Object> judging by your JSON String. If this is not a single object in your response, you should start and end your JSON String with []. This should look something like:

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}]}]';
1
  • you are right. I skipped the : from the JSON object. You are also correct, that this is not a single object. See edit to my actual JSON response as received from the end point through postman. Jul 5, 2019 at 13:26
2

You need to wrap your JSON in "[" and "]" because you have asked to deserialize a list of wrappers, not just a single wrapper instance.

0
0

One approach is to use an outer wrapper class that is a list of JSONWrapperController

e.g.

class OuterWrapper {
    JSONWrapperController[] caPerformanceRecordList;
}

class JSONWrapperController {
    String motorDealerId;
    ... // other properties
}

OuterWrapper result;

String jsonResponse = '{"caPerformanceRecordList" : [{"motorDealerID":"XL1HX", ...},{"motorDealerID":"XL1HX", ...}]}';

result = (OuterWrapper) Json.deserialize(jsonResponse,OuterWrapper.class);

for (JsonWrapperController jwc : result.caPerformanceRecordList) {
   ...
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .