title: deserializing a JSON object with multiple items inside it
body: <p>I'm trying to deserialize (using gson) a JSON object that looks like this: </p>
  
  <pre><code>        "attachments": {
              "40": {
                  "ID": 40,
                  "URL": "http:\/\/drewmore4.files.wordpress.com\/2013\/02\/wreckit.jpg",
                  "guid": "http:\/\/drewmore4.files.wordpress.com\/2013\/02\/wreckit.jpg",
                  "mime_type": "image\/jpeg",
                  "width": 287,
                  "height": 400
              },
              "3": {
                  "ID": 3,
                  "URL": "http:\/\/drewmore4.files.wordpress.com\/2013\/02\/frankenweenie2bposter.jpg",
                  "guid": "http:\/\/drewmore4.files.wordpress.com\/2013\/02\/frankenweenie2bposter.jpg",
                  "mime_type": "image\/jpeg",
                  "width": 273,
                  "height": 400
              }
      },
  </code></pre>
  
  <p>How do I handle it? I don't even know what to call this - There are multiple "items" represented here, but it's not an array. When I try to deserialize it as an array, the program crashes on a "Expected Begin_Array but found Begin_Object" exception. When I try to deserialize it as a Strong object (see class below), the program runs but the fields all return null. </p>
  
  <p>Here is the class I've tried to map it to: </p>
  
  <pre><code>class Attachment {
  int ID;
  String URL;
  }
  </code></pre>
  
  <p>the full JSON file can be seen  <a href="https://public-api.wordpress.com/rest/v1/sites/drewmore4.wordpress.com/posts/?pretty=1">here:</a></p>
  
  <p>EDIT: SOLVED. </p>
  
  <p>@Perception's solution basically worked. It was complicated by the fact that this "element" (still would like to know what this multiple-entry/non-array json element) is embedded in a larger json object that does contain an array. Again, this JSON is not my design - it comes from the Wordpress REST API, and (as @Perception alluded to), I think the issue I've had illustrates a design flaw in it - namely that the attachments element should be an array, rather than a single object. Nonetheless, </p>
  
  <p>Nonetheless, if anyone else ever needs to deserialize the result of a query for all posts on a given site using this API, and further needs access to the attachments on each post, here's how you do it: </p>
  
  <pre><code>  private class getAll extends AsyncTask &lt;Void, Void, JSONObject&gt; {
      private static final String url = "https://public-api.wordpress.com/rest/v1/sites/drewmore4.wordpress.com/posts/";
      @Override
      protected JSONObject doInBackground(Void... params) {
  
          HttpClient httpclient = new DefaultHttpClient();
          HttpGet httpget = new HttpGet(url);
          httpget.addHeader("accept", "application/json");
          JSONObject returned = new JSONObject();
          HttpResponse response;
  
          try {
              response = httpclient.execute(httpget);
              HttpEntity entity = response.getEntity();
  
              if (entity != null) {
                  InputStream instream = entity.getContent();
                  String result= convertStreamToString(instream);
  
                  returned =new JSONObject(result);                   
                  instream.close();
              }
          } 
          catch (ClientProtocolException | IOException | JSONException e) { e.printStackTrace();} 
  
          return returned;
      }
  
          @Override
      protected void onPostExecute (JSONObject returned){
          Gson gson = new Gson();
  
  //posts is the element within the JSONObject that is an array of post objects
          try {
          JSONArray posts = returned.getJSONArray("posts");
  
          for (int curr = 0; curr &lt; posts.length(); curr++){
              String s = posts.get(curr).toString();
  
              Article a = gson.fromJson(s, Article.class);
  
              JSONObject attachments = new JSONObject(s).getJSONObject("attachments");
              final Iterator&lt;String&gt; keys = attachments.keys();
                 while(keys.hasNext()) {
                      final String key = keys.next();
                      a.attached.add(gson.fromJson(attachments.getJSONObject(key).toString(), Attachment.class));
                  }   
                 stories.add(a);
          }
  
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  </code></pre>