title: Converting Gson to Object?
body: <p>I'm trying use Gson to use with a JSON return of a Web Service. That is JSON return this format: </p>
  
  <pre><code>{
    "cod":999,
    "msg":"User Data.",
    "return":{
      "ID":"74",
      "name":"FERNANDO PAIVA",
      "devices":[]
    }
  }
  </code></pre>
  
  <p>When I try use <code>gson.fromJson(json_return, MyObject.class</code> doesn't works and thrown an exception.
  I'm follow this user guide: <a href="https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson" rel="nofollow">https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson</a></p>
  
  <p>How can I do this ?</p>
  
  <p>I'm trying this.</p>
  
  <pre><code>public class User implements Serializable{
      private static final long serialVersionUID = 1L;
  
      @SerializedName("ID")
      private Long id;
  
      private String name;
  
      //get and set
  }
  
  /** get informations of web service */
  public static String get(String url){       
          String s = null;
  
          try {
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpResponse httpResponse = httpClient.execute(new HttpGet(url));
              HttpEntity entity = httpResponse.getEntity();
              if(entity != null){
                  s = EntityUtils.toString(entity);                           
              }           
          } catch (ClientProtocolException e) {           
              e.printStackTrace();
          } catch (IOException e) {           
              e.printStackTrace();
          } 
          return s;   
      }
  
  
  /** return object User */    
  public User getObjectById(){
      String url = "mydomain.com.br/script.php?email=myemail@domain.com";;
      String response = HttpClient.get(url);
      Gson gson = new Gson();
      ResponseUser ru = gson.fromJson(response, ResponseUser.class);
      User u = ru.getUser();
  
      return u;
  }
  
  
  public class ResponseUser {
  
      @SerializedName("return")
      private User user;
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }       
  }
  </code></pre>
  
  <p><strong>Exception</strong>   </p>
  
  <pre><code>11-19 12:56:23.281: E/AndroidRuntime(1481): FATAL EXCEPTION: main
  11-19 12:56:23.281: E/AndroidRuntime(1481): Process: br.com.package.myapp, PID: 1481
  11-19 12:56:23.281: E/AndroidRuntime(1481): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.package.myapp/br.com.package.myapp.act.MainActivity}: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.os.Handler.dispatchMessage(Handler.java:102)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.os.Looper.loop(Looper.java:136)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.app.ActivityThread.main(ActivityThread.java:5017)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at java.lang.reflect.Method.invokeNative(Native Method)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at java.lang.reflect.Method.invoke(Method.java:515)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at dalvik.system.NativeStart.main(Native Method)
  11-19 12:56:23.281: E/AndroidRuntime(1481): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at com.google.gson.Gson.fromJson(Gson.java:803)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at com.google.gson.Gson.fromJson(Gson.java:768)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at com.google.gson.Gson.fromJson(Gson.java:717)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at com.google.gson.Gson.fromJson(Gson.java:689)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at br.com.package.myapp.dao.UsuarioDAO.getObjectById(UsuarioDAO.java:46)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at br.com.package.myapp.act.MainActivity.onCreate(MainActivity.java:27)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.app.Activity.performCreate(Activity.java:5231)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     ... 11 more
  11-19 12:56:23.281: E/AndroidRuntime(1481): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
  11-19 12:56:23.281: E/AndroidRuntime(1481):     ... 20 more
  </code></pre>