【问题标题】:flexjson.JSONException Don't know how to bindflexjson.JSONException 不知道怎么绑定
【发布时间】:2018-03-24 03:31:19
【问题描述】:

我的 android 应用程序使用 flexjson 库来反序列化通过 RESTfull web 服务从服务器接收到的 json 数据字符串。但是当反序列化我得到错误。

flexjson.JSONException: [ data.status ]: 不知道如何将 Clock_in 绑定到 wfa.com.tma.wfa.type.TimeClockStatus 类中。您可能需要使用 ObjectFactory 而不是普通类。

这是我的字符串

{
  "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
}

`和反序列化代码

和枚举类

public enum TimeClockStatus
{
Clock_in, Clock_out;
}

【问题讨论】:

    标签: java android json flexjson


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2012-01-17
      • 1970-01-01
      • 2012-02-27
      • 2015-12-31
      • 1970-01-01
      • 2011-04-14
      • 2021-07-19
      • 2014-02-05
      • 1970-01-01
      相关资源
      最近更新 更多