title: Java: Looping through nested JSON Object
body: <p>I have the following structure for storing messages:</p>
  
  <pre><code>{
      "Channel_12": [
          ["[{\"to\":\"Bill\",\"msg\":\"Hello\",\"time\":\"10:36\"}]"], "[{\"agentName\":\"demo\",\"msg\":\"Hello 2\",\"time\":\"10:37\"}]"
      ],
      "Channel_34": [
          ["[{\"to\":\"Bill\",\"msg\":\"Hey 1\",\"time\":\"10:37\"}]"], "[{\"agentName\":\"demo\",\"msg\":\"Hey 2\",\"time\":\"10:38\"}]"
      ] }
  </code></pre>
  
  <p>I'm now trying to loop through and get each message for a given channel, lets say i want to fetch all the messages for channel <code>Channel_12</code>.</p>
  
  <p>Currently i have the following code:</p>
  
  <pre><code>        org.codehaus.jettison.json.JSONArray c = obj.getJSONArray("Channel_12");
  
          for (int i = 0 ; i &lt; c.length(); i++) {
              JSONObject obj = c.getJSONObject(i); &lt;!-- Exception is here
  
              System.out.println(obj.getString("to").toString());
              System.out.println(obj.getString("msg").toString());
  
          }
  </code></pre>
  
  <p>But am getting the following exception:</p>
  
  <pre><code>org.codehaus.jettison.json.JSONException: JSONArray[0] is not a JSONObject.
      at org.codehaus.jettison.json.JSONArray.getJSONObject(JSONArray.java:277)
      at im.Client$HistoryJSON.getHistory(Client.java:7229)
      at im.Client$32$4.run(Client.java:2246)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
      at java.awt.EventQueue.access$400(EventQueue.java:82)
      at java.awt.EventQueue$2.run(EventQueue.java:676)
      at java.awt.EventQueue$2.run(EventQueue.java:674)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
  </code></pre>
  
  <p>Any ideas?</p>