jhericks
jhericks

Reputation: 5971

Apex JSON parsing error

I am getting the following error in my code:

System.JSONException null Don't know the type of the Apex object to deserialize at [line:1, column:1] 11 (System Code) Class.FactorLab.FactorLabWebservices.RetrieveUsersFromFactorLab: line 60, column 1 Class.FactorLab.PullUsers.execute: line 61, column 1

Here is the code in question:

public static List<FactorLabPullUser> RetrieveUsersFromFactorLab(List<String> ids){ 
    HttpRequest req = getHttpRequest(baseUrl + '/ws/sfdc/people/retrieve', 'POST');        
    req.setBody(JSON.serialize(ids));
    req.setHeader('Content-Type', 'application/json');
    if(Test.isRunningTest()){
        return null;
    }        
    HttpResponse res = sendRequest(req);
    // This is the line with the error
    List<FactorLabPullUser> flusers = (List<FactorLabPullUser>)JSON.deserialize(res.getBody(), Type.forname('List<FactorLabPullUser>')); 

    return flusers;
}    

I'm sure that it's receiving valid JSON, but I'm not sure the exact JSON it's receiving when it gets this error. It could simply get an empty array:

[]

It could also get something like this:

[ { "SFDCStatus" : "RETRIEVED",
    "address" : "123 Fake St.",
    "addressLine1" : "Address1",
    "addressLine2" : "Address2",
    "city" : "San Francisco",
    "companyName" : "Big Cheese, Inc.",
    "deleted" : false,
    "email" : "[email protected]",
    "firstName" : "James",
    "hireDate" : "2000-04-15T00:00:00Z",
    "id" : 39,
    "lastName" : "Willard",
    "lastUpdated" : "2011-11-23T05:44:03Z",
    "myersBriggs" : "SFDC",
    "name" : "James Willard",
    "phone" : "415-555-1212",
    "position" : "Big Chief",
    "regions" : [ { "deleted" : false,
          "id" : 445,
          "name" : "Mountain"
        } ],
    "state" : "CA",
    "yearsExp" : 20.0,
    "zip" : "94131"
  },
  { "SFDCStatus" : "RETRIEVED",
    "deleted" : false,
    "firstName" : "Daniel",
    "id" : 40,
    "lastName" : "Adams",
    "lastUpdated" : "2011-11-23T05:44:03Z",
    "name" : "Daniel Adams"
  }
]

Any ideas? Someone told me this was a Salesforce.com bug, but it seems like even if that were true, there must be a workaround.

Upvotes: 1

Views: 12842

Answers (2)

Adam
Adam

Reputation: 2605

Looks like you may not be casting properly. I haven't played around with the serialize/deserialize native JSON much yet and have preferred to do my own parsing/persisting. But your line causing the error looks suspicious:

List<FactorLabPullUser> flusers = (List<FactorLabPullUser>)JSON.deserialize(res.getBody(), Type.forname('List<FactorLabPullUser>')); 

I think it should look like the following:

List<FactorLabPullUser> flusers = (List<FactorLabPullUser>)JSON.deserialize(res.getBody(), List<FactorLabPullUser>.class);

Upvotes: 3

Simon Goodyear
Simon Goodyear

Reputation: 416

I can't see for sure from your code snippit but if the FactorLabPullUser class is an inner class you will need to fully qualify it for Type.forName() to work.

I would expect to see something like this:

List<FactorLabPullUser> flusers = (List<FactorLabPullUser>)JSON.deserialize(res.getBody(), Type.forname('List<Outerclass.FactorLabPullUser>')); 

I think that the bug you mention is to do with the use of the .class method - I have noticed this not always return a valid Type.

Upvotes: 0

Related Questions