title: Using Gson to convert Json into Java Object
body: <p>I am currently working on a project where I am using the Steam Web API. The API returns data in JSON format and I wanted to make a Java program that uses this data. Here is the following JSON format from the API:</p>
  
  <pre><code>{
      "response": {
          "players": [
              {
                  "steamid": "---",
                  "communityvisibilitystate": 1,
                  "profilestate": 1,
                  "personaname": "---",
                  "lastlogoff": 1429915502,
                  "profileurl": "---",
                  "avatar": "---",
                  "avatarmedium": "---",
                  "avatarfull": "---",
                  "personastate": 0
              }
          ]
  
      }
  }
  </code></pre>
  
  <p>I am using Google's JSON API called Gson and I am having trouble setting up the Java classes so that I can use the <code>fromJson()</code> method.</p>
  
  <p>From the JSON data, I know that there is an array of <code>players</code> objects that contain all the data. The one thing that is confusing me is the outer tag called <code>response</code>. I know that I have to construct a class representing the <code>players</code> objects but do I also have to create a class that represents <code>response</code> since it encloses <code>players</code>? </p>
  
  <p>As of right now, I have a file named <code>Response.java</code> that contains the following:</p>
  
  <pre><code>public class Response {
  
      private ArrayList&lt;Player&gt; playerSummaries = new ArrayList&lt;Player&gt;();
  
      public String toString() {
          return playerSummaries.get(0).toString();
      }
  
  }
  
  
  class Player {
      private String steamid;
      private String personaname;
  
  
      public String getSteamID() {
          return steamid;
      }
  
      public void setSteamID(String newSteamID) {
          steamid = newSteamID;
      }
  
      public String getPersonaName() {
          return personaname;
      }
  
      public void setPersonaName(String name) {
          personaname = name;
      }
  
      //Rest of getters and setters omitted.
  
      @Override
      public String toString() {
          return "&lt;&lt;" + "steamid=" + steamid + "\n" + "name=" + personaname + "\n" + "&gt;&gt;";
      }
  }
  </code></pre>
  
  <p>I only included the variables that I plan to use. The JSON data above is contained in a String called <code>jsonData</code> and this is what I am testing in my main method:</p>
  
  <pre><code>Response response = gson.fromJson(jsonData, Response.class);
  
  System.out.println(response.getPlayerAt(0));
  </code></pre>
  
  <p>However, running this gives me an IndexOutOfBoundsException. It seems as if Gson was unable to get the information and store it into an object? Could anyone shed some light on this problem I am having? I am mostly curious to know if I have set up my classes correctly.</p>