title: Parsing JSON data and populating ListView
body: <p>I am getting issue while parsing json Data and populating list view. </p>
  
  <p>For an example below is the json data i am trying to parse and the rest of sample code which shows what i am trying to achieve.</p>
  
  <pre><code>[
      {
          "id": 123,
          "name": "abcd",
          "department": {
              "name": "xyz",
              "code": 4536
          }
      },
      {
          "id": 123,
          "name": "abcd",
          "department": {
              "name": "xyz",
              "code": 4536
          }
      }
  ]
  </code></pre>
  
  <p>This is my main Activity class</p>
  
  <pre><code>public class MainActivity extends Activity {
      private ListView employeeListView;
      private Employee employees[];
      private ArrayAdapter&lt;Employee&gt; employeeArrayAdapter;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          employeeListView=(ListView)findViewById(R.id.employeeListView);
      }
  
      public void updateListView(){
          String a="[{\"id\":123,\"name\":\"abcd\",\"department\":{\"name\":\"xyz\",\"code\":4536}},{\"id\":123,\"name\":\"abcd\",\"department\":{\"name\":\"xyz\",\"code\":4536}}]";
          try{
              JSONArray jsonArray=new JSONArray(a);
              JSONObject jsonObject;
              employees=new Employee[jsonArray.length()];
              Employee employee;
              Employee.Department department;
              int x=0;
              while(x&lt;jsonArray.length()){
                  jsonObject=jsonArray.getJSONObject(x);
                  employee=new Employee();
                  department=employee.new Department();
                  employee.setCode(jsonObject.optInt("id"));
                  employee.setName(jsonObject.optString("name"));
                  JSONObject jsonDepartment=jsonObject.getJSONObject("department");
                  department.setDepartmentName(jsonDepartment.optString("name"));
                  department.setDepartmentCode(jsonDepartment.optInt("code"));
                  employee.setDepartment(department);
                  employees[x]=employee;
                  x++;
              }
              employeeArrayAdapter=new ArrayAdapter&lt;Employee&gt;(getApplicationContext(),android.R.layout.simple_list_item_1,employees);
              employeeListView.setAdapter(employeeArrayAdapter);
          }catch (Throwable t){
              Toast.makeText(getApplicationContext(),t.toString(),Toast.LENGTH_LONG).show();
          }
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      public void onButtonClick(View view) {
          updateListView();
      }   
  }
  </code></pre>
  
  <p>And this is my pojo class</p>
  
  <pre><code>public class Employee {
      private int code;
      private String name;
      private Department department;
     //getter setter for code, name and Department
      public String toString(){
          return this.name+" "+this.code;
      }
  
  
      public class Department{//inner class
          int departmentCode;
          String departmentName;
         //getter setter for departmentCode and departmentName
      }
  }
  </code></pre>
  
  <p>OnButtonClick function i am calling updateListView which parses the json string and populate the list view. The problem here is i am unable to get Department data in list view whilst Employee data is getting populated correctly. </p>
  
  <p>Here is the snip of emulator showing the list view i am getting after running code.
  <img src="https://i.stack.imgur.com/qpmX8.png" alt="enter image description here"></p>
  
  <p>Can anyone correct me where i am going wrong. How can i display department data also?</p>