title: How do I parse a JSON array into different objects using Jackson on Android?
body: <p>I'm trying to parse JSON like the following into object using Jackson on Android (Note: I'm not in control of the JSON format - the format comes from Yammer)</p>
  
  <pre><code>"references": [
      {
          "type": "user",
          "id": 12345678,
          "name": "Wex"
      },
      {
          "type": "message",
          "id": 12345679,
          "body":
          {
              "plain":"A short message"
          }
      },
      {
          "type": "thread",
          "id": 12345670,
          "thread_starter_id": 428181699
      }
  ]
  </code></pre>
  
  <p>The problem is that each entry in <code>references</code> is a different type of object with different properties. As a start I've got:</p>
  
  <pre><code>public static class Reference
  {
      public String type;
      public String id;
  }
  </code></pre>
  
  <p>I'd rather avoid putting all potential properties in one object like:</p>
  
  <pre><code>public static class Reference
  {
      public static class Body
      {
          public String plain;
      }
      public String type;
      public String id;
      public String name;
      public Body body;
      public String thread_starter_id;
  }
  </code></pre>
  
  <p>And want to use separate classes that are created dependant on the <code>type</code> value like:</p>
  
  <pre><code>public static class ReferenceUser extends Reference
  {
      public String name;
  }
  
  public static class ReferenceMessage extends Reference
  {
      public static class Body
      {
          public String plain;
      }
      public Body body;
  }
  
  public static class ReferenceThread extends Reference
  {
      public String thread_starter_id;
  }
  </code></pre>
  
  <p>So... what's the best way of getting Jackson to parse the JSON like that?</p>
  
  <p>I'm currently parsing it quite simply like this:</p>
  
  <pre><code>ObjectMapper mapper = new ObjectMapper();
  Reference[] references = mapper.readValue(json, Reference[].class);
  </code></pre>