title: Android : parsing large json response
body: <p>I am using google <a href="http://code.google.com/p/google-gson/downloads/detail?name=google-gson-2.2.2-release.zip" rel="nofollow">gson-2.2.1</a> library for parsing large response of JSON.</p>
  
  <p>I have to parse a JSON response where structure may vary.</p>
  
  <p>First case, when the response contains more than one team:</p>
  
  <pre><code> "Details":{
  
             "Role":"abc",
             "Team":[
                {
                   "active":"yes",
                   "primary":"yes",
                   "content":"abc"
                },
                {
                   "active":"yes",
                   "primary":"yes",
                   "content":"xyz"
                }
             ],
  </code></pre>
  
  <p>Second case, when only one team is passed:</p>
  
  <pre><code>"Details":{
  
             "Role":"abc",
             "Team":
                {
                   "active":"yes",
                   "primary":"yes",
                   "content":"abc"
                }
  }
  </code></pre>
  
  <p>There are my base classes used for parsing:</p>
  
  <pre><code>class Details {
      public String Role;
      public ArrayList&lt;PlayerTeams&gt; Team = new ArrayList&lt;PlayerTeams&gt;();
          PlayerTeams Team; // when JsonObject
  }
  
  class PlayerTeams {
      public String active;
      public String primary;
      public String content;
  }
  </code></pre>
  
  <p>The problem is that I can not use <code>ArrayList&lt;PlayerTeams&gt;</code> when I have only one of them and it's returned as JsonObject.</p>
  
  <p>Gson can identify static format of JSON response. I can trace full response dynamically by checking if "Team" key is instance of <code>JsonArray</code> or <code>JsonObject</code> but it would be great if a better solution is available for that.</p>
  
  <p><b>Edit :</b>
  If my response is more dynamic..</p>
  
  <pre><code>"Details":{
  
         "Role":"abc",
         "Team":
            {
               "active":"yes",
               "primary":"yes",
               "content":"abc"
               "Test":
               {
                   "key1":"value1",
                   "key2":"value2",
                   "key3":"value3"
               }
            }
  }
  </code></pre>
  
  <p>In my edited question, I am facing problem while my response is more dynamic..<code>Team</code> and <code>Test</code> can be <code>JsonArray</code> or <code>JsonObject</code>.. It really harassing me because sometime <code>Test</code> object may array when more data, may object when single data, string when no data. There is no consistency in response.</p>