有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java flexjson。JSONException不知道如何绑定

我的安卓应用程序使用flexjson库对通过RESTfull webservice从服务器接收的json数据字符串进行反序列化。但当反序列化时,我会出错

flexjson.JSONException: [ data.status ]: Don't know how to bind Clock_in into class wfa.com.tma.wfa.type.TimeClockStatus. You might need to use an ObjectFactory instead of a plain class.

这是我的绳子

{
  "data":
     {
       "todayShift":
           {
              "startTime":"1970-01-01T13:30:00Z",
              "endTime":"1970-01-02T00:00:00Z",
              "office":
                    {
                       "id":1,"name":"Location Test 1"
                    }
          },
       "currentTime":"2017-10-12T07:47:11Z",
       "status":"Clock_in",
       "latestTime":"2017-10-12T07:46:13Z",
       "latestOffice":{"id":1,"name":"Location Test 1"}},
       "meta":{"type":"TimeClockDto"}
 }

这个字符串的DTO

public class TimeClockDto implements Serializable
{
    private static final long serialVersionUID = 1L;
    private TodayShiftDto todayShift;
    private String currentTime;
    private TimeClockStatus status;
    private String latestTime;
    private BaseLocationDto latestOffice;
    public TimeClockDto(TodayShiftDto todayShift, String currentTime, 
    TimeClockStatus status, String latestClockInTime,
                        BaseLocationDto latestOffice)
    {
        this.todayShift = todayShift;
        this.currentTime = currentTime;
        this.status = status;
        this.latestTime = latestClockInTime;
        this.latestOffice = latestOffice;
    }
    //getter, setter
}

“还有反序列化代码

还有enum类

public enum TimeClockStatus
{
Clock_in, Clock_out;
}

共 (1) 个答案

  1. # 1 楼答案

    我已经用EnumObjectFactory解决了这个问题

    TimeClockDto result = new JSONDeserializer<TimeClockDto>
                            .use("data", TimeClockDto.class)
                            .use("data.todayShift", TodayShiftDto.class)
                            .use("data.status", new EnumObjectFactory())
                            .use("data.latestOffice", BaseLocationDto.class)
                            .deserialize(param.getResult(),TimeClockDto.class);