Retrofit2 handle JSON response that contains two models data
<p>I'm using retrofit2 to handle http request after calling from API. Let me explain this.</p>

<p>I have <strong>2</strong> java class(POJO) created to handle user and lecturer data which is <code>User.java</code> and <code>Lecturer.java</code> respectively. For the response data such as :</p>

<pre><code>{
  "users": [
    {
      "user_id": "28",
      "user_email": "john@abc.com",
      "user_password": "123"
    }
  ]
}
</code></pre>

<p>i can use <code>User.java</code> class to handle this response. Nothing complex in this file, only contains <code>getter</code> and <code>setter</code> method. Same goes to lecturer data, here is the example of lecturer data :</p>

<pre><code>{
  "lecturers": [
    {
      "lecturer_id": "3",
      "user_id": "28",
      "lecturer_name": "johny2"
    }
  ]
}
</code></pre>

<p>i can handle it by using <code>Lecturer.java</code> class.</p>

<p>But the problem is, if the response contains both user and lecturer data on a single json, how to handle it?? . Here is the example of request :</p>

<pre><code>{
  "users": [
    {
      "user_id": "28",
      "user_email": "john@abc.com",
      "user_password": "123",
      "lecturer_id": "3",
      "lecturer_name": "johny2"
    }
  ]
}
</code></pre>

<p>To solve this problem, i think i need to create another java class that contains both <code>User</code> and <code>Lecturer</code> class on it, unfortunately at here i'm stuck. </p>

<p>This is new file, that i tried to create <em>(Userlecturer.java)</em> :</p>

<pre><code>public class UserLecturer {

  User user;
  Lecturer lecturer;

  // how to implement on this part
}
</code></pre>

<p>Here is <code>UserLecturer</code> interface :</p>

<pre><code>public interface UserLecturerInterface {

  @GET ( "api/endpoint/here" )
  Call&lt;UserLecturer&gt; getLecturerByUserId (@Path( "userId" ) String userId );

}
</code></pre>

<p>Appreciated for any helps. Ask me for more inputs if above use case did't clear enough. Thanks</p>