2

I'm trying to deserialize a JSON response into a list, so that eventually I can iterate through the list and create new contacts from the response.

This is my first time working with APIs and JSON, I'm getting a good response from the webservice and now I need to deserialize it into a list, every time I try to put it into the object, I wind up with 50 null objects (I'm specifically calling for 50 results in my API request). And if I try to debug a specific attribute like "id" I get an error saying it doesn't exist.

What am I doing wrong to get the info into the list object?

Here's an example of the JSON response:

[{
"id":1,
"email":"janestudent@example.edu", 
"name":"Jane Student",
"university":"University of Examples",
"comment":"I'm super interested",
"address":"",
"program_id":3558,
"program_name":3558,
"program_link":"other.example.com",
"created_at_string":"02/11/2014",
"catalog_requested":"No",
"phone_call_requested":"No",
"phone_number":"",
"lead_type":"Application"
},{
"id":2,
"email":"joe@student.edu",
"name":"Joe Student",
"university":"example University",
"comment":null,
"address":"",
"program_id":342,
"program_name":342,
"program_link":"/example.com",
"created_at_string":"02/16/2014",
"catalog_requested":"No",
"phone_call_requested":"No",
"phone_number":"",
"lead_type":"Info"
}]

Here's my deserialize, wrapper, and list classes.

String body=response.getbody();
    List<saJSON> saj=(List<saJSON>) JSON.deserialize(body,List<saJSON>.class);
    for (saJSON xx:saj) {
    System.debug(xx); //the debug that prints out null :(
}


public class saJSON {
    public List<saJSONwrapper> saJSON;
}
public class saJSONwrapper {
    String id;
    String email;
    String name;
    String university;
    String comment;
    String address;
    String program_id;
    String program_name;
    String program_link;
    String created_at_string;
    String catalog_requested;
    String phone_call_requested;
    String phone_number;
    String lead_type;

   }

Things I've tried: using deserializeStrict which results in being told "id" doesn't exist, putting the keyword public in front of the strings (nothing changes, everything is still null), deserializing into a singular object instead of a list (it told me to remove the leading and ending brackets from the json string and then it only caught the first JSON entry), changing the fields that don't have quotations to Integers (long shot), I've tried a few other things I've found on this board and some others and haven't had much success.

I apologize if I'm stating anything wrong, I'm teaching myself and mainly working off of blogs and boards for information so I may have a few things confused.

3

I think you just need to use saJSONwrapper class when you deserialize instead of saJSON:

List<saJSON> saj=(List<saJSONwrapper >) JSON.deserialize(body,List<saJSONwrapper >.class);

because in your JSON you have list of saJSONwrapper not saJSON.

  • you're right, I actually had to change everything to saJSONwrapper and it worked like this: List<saJSONwrapper> saj=(List<saJSONwrapper>) JSON.deserialize(body,List<saJSONwrapper>.class); – mikesalty Nov 3 '14 at 21:53

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.