Apex JSON分析错误

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" : "james.willard@fake.com",
    "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.

#0

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);

#1

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.

推荐文章

调试GCC编译时

调试GCC编译时

推荐文章

在MySQL中避免外键关系的麻烦

在MySQL中避免外键关系的麻烦

推荐文章

在文件夹中使用Rails模型之间的关系

在文件夹中使用Rails模型之间的关系

推荐文章

在Android上引用JNI库中预先构建的共享库

在Android上引用JNI库中预先构建的共享库

推荐文章

使用javascript显示所有书签

使用javascript显示所有书签

推荐文章

aspectj和spring与aspectj autoproxy

aspectj和spring与aspectj autoproxy

推荐文章

在iPhone OpenGL或Coregraphics中画一个光滑圆的最佳方法是什么?

在iPhone OpenGL或Coregraphics中画一个光滑圆的最佳方法是什么?

推荐文章

安卓触摸动画效果

安卓触摸动画效果

推荐文章

JQuery方法的奇怪情况&元素定位

JQuery方法的奇怪情况&元素定位

推荐文章

Scala Actor子类不能访问mailboxSize?

Scala Actor子类不能访问mailboxSize?

推荐文章

如何从Oracle Spatial中读取不适合.NET十进制类型的坐标值?

如何从Oracle Spatial中读取不适合.NET十进制类型的坐标值?

推荐文章

从应用程序包读取文本文件

从应用程序包读取文本文件

推荐文章

PHP+MySQL网站发布时的字符集问题

PHP+MySQL网站发布时的字符集问题

推荐文章

存储表视图元素的最佳方法?

存储表视图元素的最佳方法?

推荐文章

如何用java代码跟踪其他应用程序的内存和cpu使用情况

如何用java代码跟踪其他应用程序的内存和cpu使用情况

推荐文章

在golang中,是否可以从类型本身获取reflect.Type,从名称获取string?

在golang中,是否可以从类型本身获取reflect.Type,从名称获取string?