0

Through the Http response I will get the output like below.

[{"CourseType":"OnlineCourse","Id":"05f932c9-1059-4495-87d9-2e7a09bd3930","Name":"Fundamentals of Contact Lenses","Description":"This course introduces","Notes":null,"ExternalId":null,"AccessDate":null,"ExpireType":0,"ExpireDuration":{"Years":0,"Months":0,"Days":0,"Hours":0},"ExpiryDate":null,"ActiveStatus":0,"TagIds":[],"ResourceIds":[],"EditorIds":[],"Prices":[],"CompetencyDefinitionIds":[],"PrerequisiteCourseIds":[],"PostEnrollmentCourseIds":[],"AllowCourseEvaluation":true,"CategoryId":null,"CertificateUrl":null,"Audience":"US","Goals":null,"Vendor":null,"CompanyCost":null,"LearnerCost":null,"CompanyTime":null,"LearnerTime":null},{.....},{........}]

Assume that response will give more course types like OnlineCourse, offline and Curriculum. Just by using if condition, i tried to filter only course type = Curriculum.

if(CourseType == 'Curriculum')

Then I will get Id of the course type. By using the ID, I will again call the httpresponse and now I will only courses with the type Both. But after filtering, I am not getting the response in proper Json format. The response is coming like {..., ...., ...} which means the json format is missing '[ ]'.

If I can able to get the [ ] also, then I will add the response to wrapper class. So, the exception is coming while I am trying to add the result to wrapper class.

Below you find my try levels.

public static List<CurriculumObjId> FrCurriculumId(List<lmsUtils.CourseDTOFrUpdate> GrId)// Gr id contains only Curriculum course type
    {
        String sessionId;
        HttpResponse response;
        List<String> outString = new List<String>();
        List<CurriculumObjId> results;
        sessionId = lmsUtils.authenticate();

        for(lmsUtils.CourseDTOFrUpdate currId : GrId)
        {
            System.debug('Curriculum id$$$$$$$$$:'+currId.Id);
            String ep = LMS_Settings__c.getValues('EndPoint').Value__c+'v1/Curriculums?id='+currId.Id;

            if (!Test.isRunningTest())
            {
                response  = lmsUtils.processRequest(ep,'','GET',sessionId);
                System.debug('ResponseCode >>>>>>>'+response.getStatusCode());
                System.debug('GroupResponseBody >>>>>>>'+response.getBody());
                outString.add(response.getBody());
            }
            else
            {
                MockHttpResponseGenerator1 mc = new MockHttpResponseGenerator1();
                response = mc.respond2(); 
            }
        }
        System.debug('outString >>>>>>>'+outString);
        if(response.getStatusCode() == 200)
        {
            results = (List<CurriculumObjId>) JSON.deserialize(response.getBody(), List<CurriculumObjId>.class);
        }

        return results;
    }

    /* DTO for Curriculum Ids */
    Global class CurriculumObjId {

        public String CurriculumGroupIds{get;set;}
        public string TagIds{get;set;}
        public Date ExpiryDate{get;set;}
        public String Id{get;set;}
        public String Name{get;set;}

        public CurriculumObjId() {}
    }
    public class TagIds {
        string TagIds{get;set;}
    }
    public class CurriculumGroupIds {
        string TagIds{get;set;}
    }
  • Does the problem occur when Test.isRunningTest() is true i.e. the MockHttpResponseGenerator1 is returning the wrong JSON or when a real response is processed? – Keith C Jul 17 '18 at 7:51
  • @KeithC, Thank you for quick response. I just tried with by removing Test.isRunningTest(). Even though I faced the same response again. – KS Kumaar Jul 17 '18 at 7:56
  • Are you saying that the problem occurs when a real response is processed and when that real response starts with the [ shown in the JSON at the start of your question? – Keith C Jul 17 '18 at 8:12
  • @KeithC, Sorry for the delay. I mean again the response is coming with {... ... } which means It does not contains [ ]. – KS Kumaar Jul 17 '18 at 8:56
  • If the response always starts with { then you need to change your parsing code to match. See json.org to understand JSON better. – Keith C Jul 17 '18 at 9:29
0

Based on the response API and few suggestions from sfdcfox, I just changed from

 if(response.getStatusCode() == 200)
        {
            results = (List<CurriculumObjId>) JSON.deserialize(response.getBody(), List<CurriculumObjId>.class);
        }  

to

if(response.getStatusCode() == 200)
        {
            results = (urriculumObjId) JSON.deserialize(response.getBody(), CurriculumObjId.class);
        }

and the wrapper class should also need changes like below.

Global class CurriculumObjId {

        public List<String> CurriculumGroupIds{get;set;}
        public string TagIds{get;set;}
        public Date ExpiryDate{get;set;}
        public String Id{get;set;}
        public String Name{get;set;}

        public CurriculumObjId() {}
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.