0

We need to deserialize the json and assign it to a wrapper class..

{

  "facet_fields": {
  "CandidateStatus": [
     "Actively Looking Qualified",
     477,
     "Passive Headhunt",
     441,
     "Unqualified",
     39,
     "Placed By Us",
     34,
     "Shortlisted",
     30,
     "Contracting For us",
     21,
     "Not Looking / Found Own Job",
     20,
     "CV Submitted and Pending Review",
     10,
     "Interviewing",
     4,
     "EX-MCP Contractor",
     3,
     null,
     655     ]  
     }
  }

Wrapper class

public class facet_fields{
    public List<String> CandidateStatus;

    public facet_fields(){
    }
}

Deserialization code:

facet_fields ff = (facet_fields) JSON.deserialize (jsonstring, facet_fields.class);

But we are always getting null.. Any ideas to deserialize it?

3

You have an outer layer in your JSON, so you need to account for that in your class:

public class Facet_fields {
    public String[] CandidateStatus;
}
public class Wrapper {
    public Facet_fields facet_fields;
}
public Wrapper parse(String jsonValue) {
    return (Wrapper) JSON.deserialize(jsonValue, Wrapper.class);
}

Whenever you get stuck, you might try using JSON2Apex to generate classes from raw JSON to get started.

  • Instead of calling JSON.parse, we have to use JSON.deserialize – Vetriselvan Manoharan Nov 30 '15 at 20:32
  • @VetriselvanManoharan You're right, I got ahead of myself. Updated this answer. Thanks for the comment. – sfdcfox Nov 30 '15 at 20:54

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.