0

I'm trying to deserialize a List of JSON objects in Apex. Here is the JSON being passed:

[
 {
  "Name":"TestUser1",
  "Interview_Information":{
     "Interviewer":"JTA Test",
     "CandidateStage":"Final ROund",
     "Recommendation":"Hire!",
     "Candidate_Name":"JTA Test",
     "Competency":[
        {
           "Competency":"Team Player",
           "Question":"Describe when you failed on a team",
           "Rating":"Advanced",
           "Candidate_Name":"JTA Test"
        }
     ]
  }
 }
]

And here is my APEX code :

public class DeserializeJSON {
 public String PDFData {get; set;}

 public DeserializeJSON() {
    PDFData = '';
 }

 public void deserialize() {
   List<DeserializeJSON.CandidateSummary> allCandidates = (List<DeserializeJSON.CandidateSummary>)JSON.deserialize(PDFData, List<DeserializeJSON.CandidateSummary>.class);
    system.debug('allCandidates = '+allCandidates);
 }

public class CandidateSummary {
    public String Name {get; set;}
    public Interview_Information Interview_Information {get; set;}
}

public class Interview_Information {
    public String Interviewer;
    public String CandidateStage;
    public String Recommendation;
    public String Candidate_Name;
    public List<Competency> Competency;
}

public class Competency {
    public String Competency;
    public String Question;
    public String Rating;
    public String Candidate_Name;
}

UPDATE : I updated my code as per the SFDC's suggestion -- none of my debug statements are showing up on dev console now.

  • This is fine, if you are not getting the debug statements then it seems like you are not calling the DeserializeJSON.deserialize() method from anywhere. DeserializeJSON deserializeJSON = new DeserializeJSON(); deserializeJSON.PDFData = YOUR_JSON; deserializeJSON.deserialize(); And this method doesn't return anything as of now, I guess you need to adjust the return type as well. – itzmukeshy7 Mar 2 at 3:08
  • Can you just more specify your requirement? do you just want to deserializeJSON.. thats it?? – Dadhich Sourav Mar 2 at 4:55
  • @itzmukeshy7 The function is being called as the debug statements were showing up before I made changes as per the changes suggested by sfdcfox - I'm confused as to why the debug line stopped showing up on the console – user67794 Mar 2 at 5:11
0

Because Interview_Information is another object. Here's what you'd change:

public class Interview {
    public String Name;
    public Interview_Information Interview_Information;
}
public class Interview_Information {
    public String Interviewer;
    public String CandidateStage;
    public String Recommendation;
    public String Candidate_Name;
    public List<Competency> Competency;
}
public class Competency {
    public String Competency;
    public String Question;
    public String Rating;
    public String Candidate_Name;
}

And:

List<DataDisplayController.Interview> allCandidates = (List<DataDisplayController.Interview>)JSON.deserialize(PDFData, List<DataDisplayController.Interview>.class);
| improve this answer | |
  • I tried it this way as well and it didn't work. None of my debug statements display on the console so I assume it's incorrect. Let me update my code above @sfdcfox – user67794 Mar 2 at 2:06
  • @user67794 Silly question... did you set the PDFData string before attempting to deserialize? It should work given your code if the string is set correctly. – sfdcfox Mar 2 at 3:38
  • Yeah, PDFData is being setup correctly. I'm printing out the value from my JS before going to the Apex controller and the value itself is correct. After I made the changes you recommended the debug line stopped showing up - which is weird. The debug lines should be showing up regardless. – user67794 Mar 2 at 5:12

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.