-1

I am having trouble deserializiting this:

{
   "backup_times":{
      "12":{
         "time":"2012-12-20 11:52:01",
         "status":"T"
      },
      "2":{
         "time":"2012-12-20 11:22:57",
         "status":"T"
      },
      "3":{
         "time":"2012-12-20 11:23:35",
         "status":"T"
      },
      "13":{
         "time":"2012-12-20 11:52:57",
         "status":"T"
      },
      "9":{
         "time":"2012-12-20 11:46:09",
         "status":"T"
      },
      "4":{
         "time":"2012-12-20 11:28:53",
         "status":"T"
      },
      "5":{
         "time":"2012-12-20 11:30:53",
         "status":"T"
      },
      "10":{
         "time":"2012-12-20 11:48:30",
         "status":"T"
      },
      "11":{
         "time":"2012-12-20 11:49:57",
         "status":"T"
      }
   }
}

Is this structure valid to translate it to a custom class?

This is my current java class that I currently have, it basically contains hashmap of a string as key and Job subclass as the value:

    public class RestoreDatesJsonReader {
    private HashMap<String, Job> backupTimes;

    public static class Job {
        private String time;
        private String status;
        public String getTime() {
            return time;
        }
        public void setTime(String time) {
            this.time = time;
        }
        public String getStatus() {
            return status;
        }
        public void setStatus(String status) {
            this.status = status;
        }
    }

    public HashMap<String,Job> getBackupTimes() {
        return backupTimes;
    }

    public void setBackupTimes(HashMap<String, Job> backup_times) {
        this.backupTimes = backup_times;
    }
}
joze
  • 66
  • 5
  • 1
    If you're asking whether or not it's valid JSON: http://jsonlint.org (it is). If you're having trouble because of the time format, you may find this helpful: http://stackoverflow.com/questions/5845822/gson-deserializing-key-value-to-custom-object/5845866#5845866 – Matt Ball Dec 20 '12 at 23:58
  • I am asking rather how can you translate that into a java custom class or bean – joze Dec 21 '12 at 00:00
  • We're not going to do the work for you. [What have _you_ tried?](http://whathaveyoutried.com) – Matt Ball Dec 21 '12 at 00:01
  • I added above my current code thanks for your help.. – joze Dec 21 '12 at 00:07
  • 1
    if you change `backupTimes` to `backup_times` I see no reason why this wouldn't map. – Brian Roach Dec 21 '12 at 00:10
  • Thanks!!! That solved it!! I have been struggling for hours! I thought you also had to change the variables names. – joze Dec 21 '12 at 00:15

1 Answers1

1

GSON (by default) relies on your POJO having variable names that match your JSON object.

Change backupTimes in your object to backup_times and this will work just fine.

Your other option is is to use the @SerializedName annotation for your field:

@SerializedName("backup_times") private HashMap<String, Job> backupTimes;

This tells Gson that backup_times in your JSON maps to backupTimes in your POJO.

(Moved this from a comment to a proper answer once I had the time to go back and do so)

Javadoc here: http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/annotations/SerializedName.html

Brian Roach
  • 76,169
  • 12
  • 136
  • 161