如何使用GSON将java对象转换为JSON字符串?

时间:2016-11-29 13:02:51

标签: android json gson

我正在尝试使用GSON将java对象转换为JSON String,但是我遇到了类型不匹配异常。

以下是我尝试将其转换为JSON字符串的类:

@DatabaseTable(tableName = "UserProfileEventDataDb")
public class UserProfileEventDataDb implements Serializable {

    private static final long serialVersionUID = 1L;

    @DatabaseField(id = true, unique = true)
    String id;
    @DatabaseField(columnName = "Profile_Schedule" ,dataType = DataType.SERIALIZABLE)
    String[] schedule;
    @DatabaseField(columnName = "Profile_Network" ,dataType = DataType.SERIALIZABLE)
    String[] network;
    @DatabaseField(columnName = "Profile_QrScans" , dataType = DataType.SERIALIZABLE)
    String[] qrScans;
    @DatabaseField(columnName = "Profile_Checkins" , dataType = DataType.SERIALIZABLE)
    String[] checkIns;
    @DatabaseField(columnName = "Profile_Surveys" , dataType = DataType.SERIALIZABLE)
    String[] surveys;
    @DatabaseField(columnName = "Profile_Polls" , dataType = DataType.SERIALIZABLE)
    String[] polls;
    @DatabaseField(columnName = "Profile_Feedbacks" , dataType = DataType.SERIALIZABLE)
    String[] post_session_feedbacks;

    @DatabaseField(columnName = "UserProfileDb", foreign = true, foreignAutoRefresh = true, dataType = DataType.SERIALIZABLE)
    transient UserProfileDb userProfileDb;

    public UserProfileEventDataDb() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String[] getSchedule() {
        return schedule;
    }

    public void setSchedule(String[] schedule) {
        this.schedule = schedule;
    }

    public String[] getNetwork() {
        return network;
    }

    public void setNetwork(String[] network) {
        this.network = network;
    }

    public String[] getQrScans() {
        return qrScans;
    }

    public void setQrScans(String[] qrScans) {
        this.qrScans = qrScans;
    }

    public String[] getCheckIns() {
        return checkIns;
    }

    public void setCheckIns(String[] checkIns) {
        this.checkIns = checkIns;
    }

    public String[] getSurveys() {
        return surveys;
    }

    public void setSurveys(String[] surveys) {
        this.surveys = surveys;
    }

    public String[] getPolls() {
        return polls;
    }

    public void setPolls(String[] polls) {
        this.polls = polls;
    }

    public String[] getPost_session_feedbacks() {
        return post_session_feedbacks;
    }

    public void setPost_session_feedbacks(String[] post_session_feedbacks) {
        this.post_session_feedbacks = post_session_feedbacks;
    }

    public UserProfileDb getUserProfileDb() {
        return userProfileDb;
    }

    public void setUserProfileDb(UserProfileDb userProfileDb) {
        this.userProfileDb = userProfileDb;
    }
}

@DatabaseTable(tableName = "UserProfileData")
public class UserProfileDb implements Serializable {

    private static final long serialVersionUID = 1L;

    @DatabaseField(id = true)
    String id;

    @ForeignCollectionField(columnName = "eventData" , eager = true)
    Collection<UserProfileEventDataDb> eventData;

    @DatabaseField(dataType = DataType.SERIALIZABLE)
    UserProfileGlobalDataDb globalData;

    public UserProfileDb() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Collection<UserProfileEventDataDb> getEventData() {
        return eventData;
    }

    public void setEventData(Collection<UserProfileEventDataDb> eventData) {
        this.eventData = eventData;
    }

    public UserProfileGlobalDataDb getGlobalData() {
        return globalData;
    }

    public void setGlobalData(UserProfileGlobalDataDb globalData) {
        this.globalData = globalData;
    }

}

JSON字符串

{
"eventData":[
  {
    "checkIns":[],
    "id":"57a30f778733b7421bbdf8e4",
    "network":[],
    "polls":[],
    "qrScans":[],
    "schedule":[
        "a0d157b0-6489-11e6-a630-a5bd59294bfd",
        "db30f5f0-5a3e-11e6-97e8-bf4392977b67",
        "e43b6ed0-6071-11e6-ada7-497b397146aa",
        "808976c0-6f53-11e6-ab77-d9904268ddbd",
        "ba2261f0-5a39-11e6-a7a9-b3c3bef6460d",
        "ba7565d0-5e1c-11e6-90b3-dde5577b0615",
        "8c3b0510-5d6f-11e6-aa35-d51d138c44a8"
      ],
    "surveys":[
        "a0d157b0-6489-11e6-a630-a5bd59294bfd|57a3238d8733b7421bbdf8f6",
        "ba2261f0-5a39-11e6-a7a9-b3c3bef6460d|57a3252f8733b7421bbdf8f7",
        "ba2261f0-5a39-11e6-a7a9-b3c3bef6460d|57a6ecf88733b7421bbdf906",
        "8c3b0510-5d6f-11e6-aa35-d51d138c44a8|57a329d58733b7421bbdf8f8"
      ]
      }
    ],
    "globalData":{
      "topics":["Class","Cheese","Demo","DemoCup","Food"]
        }
  }

请帮帮我。提前谢谢。

0 个答案:

没有答案