title: converting gson to object with java
body: <p>I'm trying to figure out how to use gson to convert reddit's api response to a form I can use. After some code I use </p>
  
  <pre><code>System.out.println(response.toString());
  </code></pre>
  
  <p>to get output (slightly edited)</p>
  
  <pre><code>{"json": {"errors": [], "data": {"modhash": "dosiom5o6abbbb758729039f04762a05778db4aeeeacd8eb4a", "cookie": "14756159,2012-08-21T12:05:05,0971bdec35d71af4073cf56ad82fb0ae7c5fe2d1"}}}
  </code></pre>
  
  <p>After googling around I built the following class</p>
  
  <pre><code>class GSONClass {
  
      private Response jsonresponse;
  
      public Response getJsonresponse() {
          return jsonresponse;
      }
  
      public void setJsonresponse(Response jsonresponse) {
          this.jsonresponse = jsonresponse;
      }
  
      public static class Response {
          private String[] errors;
          private Data data;
  
          public Data getData() {
              return data;
          }
  
          public void setData(Data data) {
              this.data = data;
          }
  
          public String[] getErrors() {
              return errors;
          }
  
          public void setErrors(String[] errors) {
              this.errors = errors;
          }
      }
  
      public static class Data {
          private String modhash = "hi";
          private String cookie;
  
          public String getCookie() {
              return cookie;
          }
  
          public void setCookie(String cookie) {
              this.cookie = cookie;
          }
  
          public String getModhash() {
              return modhash;
          }
  
          public void setModhash(String modhash) {
              this.modhash = modhash;
          }
      }
  }
  </code></pre>
  
  <p>Then I use:</p>
  
  <pre><code>GSONClass target = new GSONClass();
  String json = gson.toJson(response.toString());
  GSONClass target = gson.fromJson(json, GSONClass.class);
  </code></pre>
  
  <p>I'm doing something wrong because I get a "java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING" error.</p>