title: How to parse JSON response (different object types) with GSON
body: <p>Problem: parse the following response from Foursquare Venues API:</p>
  
  <pre><code>{
      meta: {
          code: 200
      }
      notifications: [
      {
          type: "notificationTray"
          item: {
          unreadCount: 0
          }
      }
      ]
      response: {
          venues: [
          {
              id: "5374fa22498e33ddadb073b3"
              name: "venue 1"
          },
          {
              id: "5374fa22498e33ddadb073b4"
              name: "venue 2"
          }
          ],
          neighborhoods: [ ],
          confident: true
      }
  }
  </code></pre>
  
  <p>The <a href="https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Collection-with-Objects-of-Arbitrary-Types" rel="nofollow">GSON documentation</a> website recommends using GSON's parse API to parse the response as a JSONArray and then read each array item into an appropriate Object or data type (Example <a href="https://code.google.com/p/google-gson/source/browse/trunk/extras/src/main/java/com/google/gson/extras/examples/rawcollections/RawCollectionsExample.java" rel="nofollow">here</a>). As such, I originally switched to the following implementation:</p>
  
  <pre class="lang-java prettyprint-override"><code>JsonParser parser = new JsonParser();
                  try {
                      JSONObject json = new JSONObject(response);
                      JSONArray venues = json.getJSONObject("response").getJSONArray("venues");
  
                      int arraylengh = venues.length();
                      for(int i=0; i &lt; arraylengh; i++){
                          Log.d(TAG, "The current element is: " + venues.get(i).toString());
                      }
                  }
                  catch(JSONException e){
  
                  }
  </code></pre>
  
  <p>The code above gave me a JSONArray with all the "venues". The next problem was that I do not know how to parse/convert the "venues" JSONArray into a ArrayList (for my custom Venue object). </p>
  
  <p><strong>Solution:</strong> As outlined on JohnUopini answer I was able to successfully parse the JSON by using the following implementation:</p>
  
  <pre class="lang-java prettyprint-override"><code>GsonBuilder gsonBuilder = new GsonBuilder();
  Gson gson = gsonBuilder.create();
  
  JsonParser parser = new JsonParser();
  JsonObject data = parser.parse(response).getAsJsonObject();
  Meta meta = gson.fromJson(data.get("meta"), Meta.class);
  Response myResponse = gson.fromJson(data.get("response"), Response.class);
  List&lt;Venue&gt; venues = Arrays.asList(myResponse.getVenues());
  </code></pre>
  
  <p>With the above code I was able to successfully parse the "meta" as well as the "response" JSON properties into my custom objects.</p>
  
  <p>For reference, below is my Response class (NOTE: The properties were defined as public for testing purposes. A final implementation should have these declared as private and use setters/getters for encapsulation):</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Response {
  
      @SerializedName("venues")
      public Venue[] venues;
  
      @SerializedName("confident")
      public boolean confident;
  
      Response(){}
  }
  </code></pre>
  
  <p><strong>Note/Feedback:</strong> After implementing the accepted answer's recommendation, a couple of times I encountered the following (or similar) exception message during my debugging process:</p>
  
  <pre><code>com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected STRING but was BEGIN_OBJECT
  </code></pre>
  
  <p>The reason I was getting the above exception was because the "type" on some of the children inside the "venues" JSON did not match with the "type" I defined such objects in my custom Venue class.  Make sure the type in your custom Classes has a 1-to-1 correspondence with the JSON (i.e. [ ] is a array property, { } is an Object property, etc).</p>